Supporting Multiple modes with Spring Portlet MVC Framework

In the HelloWorld Spring Portlet MVC Framework 3.0.1 entry i did build a simple HelloWorld Spring portlet which supports only VIEW mode.

Today i built a MultipleModesPortlet to demonstrate how you can support more than one mode using Spring MVC Framework. You can download the sample code from here

The MutlipleModesPortlet, support three modes VIEW, EDIT and HELP, It has one controller class for every mode. Ex ViewController class will handle every request for VIEW mode, the EditController class will handle every request for EDIT mode and HelpController class will handle every request for help mode. Only thing that Controller does is forward control to a JSP for rendering markup. The ViewController forwards control to view.jsp and similarly the other controller forward control to corresponding JSP.

This is how my ViewController.java looks like

public class ViewController extends AbstractController{
protected ModelAndView handleRenderRequestInternal(RenderRequest request,
RenderResponse response) throws Exception {
System.out.println("Entering ViewController.handleRenderRequest()");
ModelAndView modelAndView = new ModelAndView("view");
System.out.println("Exiting ViewController.handleRenderRequest()");
return modelAndView;
}
}


As you can see the ViewController class overrides handleRenderRequestInternal() method which gets call every time user tries to access the portlet in VIEW mode. Inside the handleRenderRequestInternal() method i am creating and returning ModelAndView("view") object. Which is equivalent to forwarding control to JSP for generating markup.

This is how my 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 from view.jsp</h1>

As you can see this JSP is generating static text.

Now the more important part is how to configure Spring Portlet MVC framework so that different controllers get called for different modes and the answer is that configuration goes in MultipleModes-portlet.xml like this

<?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="helpController"
class="com.webspherenotes.mvc.spring.action.HelpController" />
<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>
<entry key="help">
<ref bean="helpController" />
</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>


As you can see first i have created three bean definitions one for each Controller, this is the Controller class for com.webspherenotes.mvc.spring.action.EditController

Then the important part is portletModeHandlerMapping which has portletModeMap map that is used for mapping a Controller class to the mode. I am mapping view mode to viewController and edit mode to editController,..

5 comments:

chandra bhanu rastogi said...

Hi.. This is very good tutorial. Can I write a single controller for multi modes.

I want to write single controller which will support all three modes.

Is it Possible??
Thanks in advance

Solmaz said...

No, its not possible. Every mode needs a separate controller

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

srjwebsolutions said...


We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

EG MEDI said...

Egmedi.com is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery


Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store