Handling actions in Spring Portlet MVC Framework

In the HelloWorld Spring Portlet MVC Framework 3.0.1 entry, i covered the steps required to create simple HelloWorld portlet, that will forward control to a JSP in the view mode, but that portlet does not support action requests. I built a HelloSpring31ActionPortlet to demonstrate how to handle action request in the portlet.

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;
    }
    }


    The EditController has two methods

    1. 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 to ActionRequest and ActionResponse object, i am using this opportunity to read the value of userName parameter submitted by user and setting it in preference

    2. handleRenderRequest: This method gets called to get markup for edit mode of the portlet. I am using it for forwarding control to edit.jsp




  • 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 to actionURL


  • 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;
    }
    }


    The ViewController class has only handleRenderRequest method and it does not support actionURl being called for view mode. In the handleRenderRequest i am reading value of the userName preference setting it in the request object by adding it in the ModelAndView 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 of userName 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 then portletModeHandlerMapping 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>


2 comments:

Youssef Elhafyani said...

hey if you trying this tutorial , don't forget to add support to the portlet modes in portlet.xml

text/html
edit

Abhi said...

Thanks for info....
SEO Company in Bangalore