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 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="" 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=""

<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"
<property name="order" value="1" />
<property name="portletModeMap">
<entry key="view">
<ref bean="viewController" />
<entry key="edit">
<ref bean="editController" />
<entry key="help">
<ref bean="helpController" />


<bean id="viewResolver"
<property name="viewClass">
<property name="prefix">
<property name="suffix">

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,..


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