The
HelloSpring31ActionPortlet
portlet supports two modes VIEW
and EDIT
. It displays a form in the EDIT
mode where user can enter value for userName
and when user submits the form it will save that value in the userName
preference. Then in the VIEW
mode it reads that value and shows a customized greeting message to the user.I had to follow these steps to build this application
First i built this EditController.java, which will be responsible for handling requests when the portlet is invoked in the EDIT mode
package com.webspherenotes.mvc.spring.action;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;
public class EditController extends AbstractController{
public void handleActionRequest(ActionRequest request,
ActionResponse response) throws Exception {
System.out.println("Entering EditController.handleActionRequest");
String userName = request.getParameter("userName");
PortletPreferences preferences = request.getPreferences();
preferences.setValue("userName", userName);
preferences.store();
response.setPortletMode(PortletMode.VIEW);
System.out.println("Exiting EditController.handleActionRequest");
}
public ModelAndView handleRenderRequest(RenderRequest request,
RenderResponse response) throws Exception {
System.out.println("Entering EditController.handleRenderRequest");
ModelAndView modelAndView = new ModelAndView("edit");
System.out.println("Exiting EditController.handleRenderRequest");
return modelAndView;
}
}
TheEditController
has two methods- handleActionRequest: This method gets called whenever you try to access the
actionURL
pointing to the Edit mode of the portlet. Inside this method you get access toActionRequest
andActionResponse
object, i am using this opportunity to read the value ofuserName
parameter submitted by user and setting it in preference - handleRenderRequest: This method gets called to get markup for edit mode of the portlet. I am using it for forwarding control to edit.jsp
- handleActionRequest: This method gets called whenever you try to access the
This how the edit.jsp page looks like
<%@page language="java" contentType="text/html; %>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<form method="POST" action="<portlet:actionURL/>">
<table>
<tr>
<td>User Name</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" /></td>
</tr>
</table>
</form>
As you can see this JSP is similar to any other portlet JSP, all that it is doing is submitting a form toactionURL
This is how the ViewController.jsp for my portlet looks like
package com.webspherenotes.mvc.spring.action;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;
public class ViewController extends AbstractController{
public ModelAndView handleRenderRequest(RenderRequest request,
RenderResponse response) throws Exception {
System.out.println("Entering ViewController.handleRenderRequest()");
ModelAndView modelAndView = new ModelAndView("view");
modelAndView.addObject("userName",
request.getPreferences().getValue("userName", "User Name is not set"));
System.out.println("Exiting ViewController.handleRenderRequest()");
return modelAndView;
}
}
TheViewController
class has onlyhandleRenderRequest
method and it does not supportactionURl
being called for view mode. In thehandleRenderRequest
i am reading value of theuserName
preference setting it in the request object by adding it in theModelAndView
and then forwarding control to view.jsp
This is how the view.jsp looks like
<%@page language="java" contentType="text/html; %>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<h1>Hello <%=renderRequest.getAttribute("userName") %></h1>
Here i am reading the value ofuserName
attribute from request and then using it build customized greeting for user
This is how the spring configuration for my portlet looks like, as you can see i have two beans for two different modes and thenportletModeHandlerMapping
is used for mapping the bean name to the mode
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="viewController"
class="com.webspherenotes.mvc.spring.action.ViewController" />
<bean id="editController"
class="com.webspherenotes.mvc.spring.action.EditController" />
<bean id="portletModeHandlerMapping"
class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
<property name="order" value="1" />
<property name="portletModeMap">
<map>
<entry key="view">
<ref bean="viewController" />
</entry>
<entry key="edit">
<ref bean="editController" />
</entry>
</map>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
hey if you trying this tutorial , don't forget to add support to the portlet modes in portlet.xml
ReplyDeletetext/html
edit
Thanks for info....
ReplyDeleteSEO Company in Bangalore