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
,..
Hi.. This is very good tutorial. Can I write a single controller for multi modes.
ReplyDeleteI want to write single controller which will support all three modes.
Is it Possible??
Thanks in advance
No, its not possible. Every mode needs a separate controller
ReplyDeleteThanks for info....
ReplyDeleteSEO Company in Bangalore