Quick Intro – Spring Portlet

Portlet application extended from web application

  1. web.xml and
  2. portlet.xml

    Request & Response

Action Request

  1. Usually used for form post or execute onces and no markup produced.
  2. Support java portlet name called.
  3. Example @ActionMapping(params = "action=myactionname")

Render Request

  1. Used to render view pages. method = GET and,
  2. Support java portlet name called.
  3. Example @RenderMapping(params = "action=myactionname")
  4. Support window state

Event Request – Portlet 2.0 intere-portlet-communication (IPC)

  1. Event request used to accept an event fire, action as listener.
  2. Parameters : name & qname
  3. Example used as intere-portlet-communication (IPC)

Resource Request – Portlet 2.0

  1. Example used as JSON-ajax

    Portlet Mode

    There are 3 standard portlet mode

  1. View – Render data or show a form for user interaction.
  2. Edit – Modify user preferences.
  3. Help – Display information to assist the user

    Handling

Handler we talking in here is controller created by you, For example : ViewBookController is a handler that implements the controller interface and included 2 methods.

  • handlerActionRequest(request, response)
  • handlerRenderRequest(request, response)

    HandlerMappings

A Handler is any Object that can handler portlet request. Controller are an example of Handlers (default). If you like to use some other framework with DispatcherPortlet, a corresponding implementation of HandlerAdapter is needed. HandlerMappings are commonly based on URLS. But there is not URL within a portlet therefore we must use other mechanisms to controller the mappings. The 2 most common are portlet mode and a request parameter, but anything available to the portlet request can be used in a custom handler mapping.

    PortletModeHandlerMapping

This is the simple handler mapping that maps incoming requests based on the current mode of portlet, view, edit, help

  1.  
  2. <bean org.springframework.web.portlet.handler.PortletModeHandlerMapping">
  3.     <property name="portletModeMap">
  4.     <map>
  5.             <entry key="view" value-ref="viewHandler" />
  6.             <entry key="edit" value-ref="editHandler" />
  7.             <entry key="help" value-ref="helpHandler" />
  8.     </map>
  9.     </property>
  10. </bean>

    ParameterHandlerMapping

Used to navigate around multiple controllers without change the portlet mode, the simples way to do this is with a request parameter that is used as the key to control the mapping

ParameterHandlerMapping use specific request parameter to control the mapping, the default name of the parameter is ‘action’ but can changed using the ‘parameterName’ property.

  1.  
  2. <bean class="org.springframework.web.portlet.handler.ParameterHandlerMapping”>
  3.      <property name="parameterMap">
  4.      <map>
  5.             <entry key="add" value-ref="addItemHandler" />
  6.             <entry key="edit" value-ref="editItemHandler" />
  7.             <entry key="delete" value-ref="deleteItemHandler" />
  8.      </map>
  9.      </property>
  10. </bean>

    PortletModeParameterHandlerMapping

As describe as the name, it combined the previous 2 handler mapping strategies to allow different navigation within each portlet mode.

  1.  
  2. <bean class="org.springframework.web.portlet.handler.PortletModeParameterHandlerMapping">
  3. <property name="portletModeParameterMap">
  4.         <map>
  5.            <entry key="view"> <!– ‘view’ portlet mode –>
  6.                 <map>
  7.                     <entry key="add" value-ref="addItemHandler"/>
  8.                     <entry key="edit" value-ref="editItemHandler"/>
  9.                     <entry key="delete" value-ref="deleteItemHandler"/>
  10.                 </map>              
  11.            </entry>
  12.            <entry key="edit"> <!– ‘edit’ portlet mode –>
  13.                 <map>
  14.                     <entry key="prefs" value-ref="prefsHandler"/>
  15.                     <entry key="resetPrefs" value-ref="resetPrefsHandler"/>
  16.                 </map>                        
  17.            </entry>
  18.         </map>
  19. </property>
  20. </bean>
  21.  

More example

    Mapping and Portlet Lifecycle

For an Action Request, the handler mapping will be consulted twice, once for action phase and agains for  render phase You can manipulate the criteria used for mapping during the action phase, eg: request parameter. This can result in the render phase getting  mapped to a different controller.

    Annotation

  1. @Controller – class stereotype for controller classes so they can be found and mapped
  2. @SessionAttributes – list model attributes to be stored in the session (command object)
  3. @RequestMapping – class/method mapping to requests (mode, parameters, etc.)
  4. @RequestParam – bind method params to request params
  5. @ModelAttribute – bind method params or return values to model attributes
  6. @InitBinder – method to setup binder for putting form submission into command obj

    Spring 3.0 supported portlet 2.0 annotation

  1. @ActionMapping - Elements: name, params
  2. @EventMapping - Elements: name, qname, params
  3. @RenderMapping - Elements: windowState, params
  4. @ResourceMapping - Elements: id, params

    Strategies in Controller

The showPage method has return String type, therefore we can assume it as render page, because it will look forward a jsp pages to render when this method is called.

Spring mvc portlet annotation use @RequestMapping strategies to map ‘EDIT’, ‘VIEW’ and ‘HELP’ mode. We can use portletModeParameterHandlerMapping create more interacting communication among the others controllers.

Example, ShowBooksController. By default annotated @Controller, @RequestMapping(“VIEW”). It will look for the method with annotated @RequestMapping method and return type String. The example below will return view page personJSPPages.jsp

Render Phase

  1. import …
  2. @RequestMapping
  3. public String showPage(Model model)
  4. {
  5.      if(!model.containsAttribute("person")) {
  6.             model.addAttribute("person",new Person());
  7.      }
  8.      return "personJSPPages";
  9. }

Action Phase

Consider the method below annotated with @RequestMapping, and return type is void, therefore it will only process at onces, such like form submission. The method below will correspond to the action = ‘add’ in the portlet ‘VIEW’ mode.

  1. import …
  2. @RequestMapping(params = "action=add")
  3. public void processSubmit(ModelAttribute("person") Person person, BindingResult result,
  4. SessionStatus status, ActionResponse response)
  5. {
  6.         new PersonValidator().validate(person, result);
  7.         if(!result.hasErrors()) {
  8.                this.person.put(person.getName(), person.getFullName());
  9.                status.setComplete();
  10.                response.setRenderParameter("action","list");
  11.         }
  12.         return "personJSPPages";
  13. }
You can leave a response, or trackback from your own site.

2 Responses to “Quick Intro – Spring Portlet”

  1. Sri says:

    Hi,

    Does @ResourceMapping accepts params option.
    I am using spring 3.0.5 portlet mvc and when I see the spring api for ResourceMapping annotation, it does say only “value” as option, not “params”.

    Anyhow I see “params”, “value” both as option in spring 3.0.0 M2 api.

    Please let me know, if you have any idea on this.

    Thanks you,
    Sri

Leave a Reply

Security Code: