{{announcement.body}}
{{announcement.title}}

Developing a JSR 286 Portlet Using Spring MVC

DZone 's Guide to

Developing a JSR 286 Portlet Using Spring MVC

A step-by-step approach to develop a JSR 286 portlet with Spring MVC using RAD 8 and deploy in WebSphere Portal 8.5

· Java Zone ·
Free Resource

Spring is a very popular framework, providing lots of features and functionalities organized in a modular fashion. In addition to supporting conventional (servlet-based) web development, Spring also supports JSR-286 Portlet development. The Portlet MVC framework is a mirror image of the Web MVC framework, and also uses the same underlying view abstractions and integration technology.

Just like any other MVC framework , Spring MVC consists of 3 things.

  1. M-model 
  2. V-view 
  3. C-Controller. 

The following is the core architecture of Spring (Web) MVC.

The Front Controller (Dispatcher Portlet) works as the Controller. The front controller takes the incoming request and dispatches to the specific handler (Controller) that has been designed to cater that particular type of request.

The handler (Controller) processes the request and sends the response back to the front controller with the help of Model.

The front controller then determines the particular View with the help of View Resolver and finally sends the response back to the client.

Now let's create a Spring MVC portlet, for example. I will use using Rational Application Developer (RAD) to create the portlet.

Step 1: Open your IDE (Rational Application Developer (RAD) or Eclipse) and create a new portlet project

Step 2: Now modify the type of the portlet and make it a JSR 286 empty portlet

This will create the structure of the web project and will look like this.

Step 3: Now add the spring library and other related JARs in our project. Download the following spring JARs from here. Add the JARs in the /WEB-INF/lib folder of the project.

Step 4: The next step is to add the entry of "View Renderer Servlet" in web deployment descriptor. This step is very important. A Spring portlet is an extension of the Spring Web MVC, and Spring provides support of all its functionalities in the portlet context with the help of this renderer servlet. This servlet works as a bridge between Spring Web MVC and Portlet MVC.

XML
 




x
9


 
1
<servlet>
2
     <servlet-name>ViewRendererServlet</servlet-name>
3
     <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
4
 </servlet>
5
 
          
6
<servlet-mapping>
7
  <servlet-name>ViewRendererServlet</servlet-name>
8
  <url-pattern>/WEB-INF/servlet/view</url-pattern>
9
</servlet-mapping>



Also, add the following entry of the Context Loader Listener. This will tie the Application Context to the life cycle of the Servlet Context and automate the creation of the Application Context. 

XML
 







Step 5: Open the portlet deployment descriptor (portlet.xml) and list the dispatcher portlet. As described above, the dispatcher portlet will serve as the front controller and will be responsible for dispatching the request to the controller specified to handle that particular type of request.

XML
 




xxxxxxxxxx
1


1
<portlet >
2
...
3
<portlet-class> org.springframework.web.portlet.DispatcherPortlet</portlet-class>
4
...
5
</portlet>



There is another entry that you can provide here in portlet.xml — "contextConfigLocation". That's in case you want to have a custom name or location for the application context file. If you do not provide any specific location, Spring will look for the Application Context file inside "/WEB-INF" by default.  

XML
 




xxxxxxxxxx
1


1
<context-param>
2
<param-name>contextConfigLocation</param-name>
3
<param-value><<location of you Application Context file>> </param-value>
4
</context-param>



Step 6: Create a file named "ApplicationContext" inside "/WEB-INF" and add the entry of "ViewResolver" there. In Spring, "DispatcherPortlet" will take the help of "ViewResolver" to choose the view (JSP in our case). So we need to configure the view (through View Resolver) in the Spring application context file. Add the following entry in the context file

XML
 




xxxxxxxxxx
1


1
<bean id="viewResolver"
2
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
3
<property name="cache" value="false" />
4
<property name="viewClass"
5
value="org.springframework.web.servlet.view.InternalResourceView" />
6
<property name="prefix" value="/WEB-INF/jsp/" />
7
<property name="suffix" value=".jsp" />
8
</bean> 



Step 7:  Now we will create another context file and place it inside WEB-INF folder. The naming convention of the context needs to be <<portletName>> - portlet.xml. So, in our case, it will be "MyFirstSpringMVCPortlet-portlet.xml". This is a context file we will maintain to place all portlet-specific configuration parameters.

Inside the config file, register your controllers and DefaultAnnotationHandlerMapping. The DefaultAnnotationHandlerMapping maps requests to classes and/or methods that are annotated with @RequestMapping.

XML
 




xxxxxxxxxx
1


1
<bean id="viewController" class="com.springmvc.poc.portlet.controller.ViewController"></bean>
2
<bean id="editController" class="com.springmvc.poc.portlet.controller.EditController"></bean>
3
<bean id="helpController" class="com.springmvc.poc.portlet.controller.HelpController"></bean>
4
 
          
5
 
          
6
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>



You may find it confusing to maintain two different config files (Application Context and MyFirstSpringMVCPortlet-portlet.xml). It is not mandatory, but I am following a good practice by separating the application-specific configuration with portlet-specific configuration.

Step 8: Now we need to create the controller class in the same package as registered in the context file.

Java
 




x


 
1
@Controller("viewController")
2
@RequestMapping(value = "VIEW")
3
public class ViewController {
4
 
          
5
  @RequestMapping
6
  protected ModelAndView handleRenderRequestInternal(RenderRequest request,
7
  RenderResponse response) throws Exception {
8
    ModelAndView modelAndView = new ModelAndView();
9
    modelAndView.setViewName("view");
10
    return modelAndView;
11
  }
12
 
          
13
}



@Controller is used to denote that this is our controller. The value will be same as class name. The second annotation is @RequestMapping(“VIEW”), which tells the DispatcherPortlet that this controller will support VIEW mode. Similarly, a separate controller has to be added for other modes (EDIT, HELP, etc.).

The @RenderMapping annotation denotes the default render method. It means whenever we place this portlet, it will render this method.

To handle render requests, action requests, or event requests, corresponding methods need to be added with the following annotations, respectively:

Java
 








In our example, the method returns a ModeAndView object. The ModelAndView object tries to resolve the view name (view.jsp) in the path we specified in the our view resolver entry in the previous step. We have specified the prefix as "/WEB-INF/jsp/" and the suffix as ".jsp". So, the DispatcherPortlet, with the help of ViewResolver, will try to find "/WEB-INF/jsp/view.jsp" and will try to render it.


Step 9: Finally, we need to create the JSP in the same location so that it can be rendered.


We will write a bit of text in the JSP to display after a successful rendering of the portlet.

HTML
 




xxxxxxxxxx
1
14


1
<%@page session="false" contentType="text/html"
2
pageEncoding="ISO-8859-1"
3
import="java.util.*,javax.portlet.*,com.freedommortgage.portlet.loandetailsheaderportlet.controller.*,com.freedommortgage.portlet.loandetailsheaderportlet.model.*"%>
4
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
5
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
6
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
7
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
8
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
9
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
10
<fmt:setBundle
11
basename="com.freedommortgage.portlet.loandetailsheaderportlet.nl.FMC_LoanDetailsHeaderPortletResource" />
12
<portlet:defineObjects />
13
 
          
14
<h3>MyFirstSpringMVCPortlet rendered successfully<h3> 



Step 10: Now we are all set to deploy and test our Spring MVC portlet. Once deployed and added to a portal page, it will render and display the JSP.


Topics:
j2ee, java, jsr 286, portlet, spring mvc

Published at DZone with permission of Soumyajit Sarkar . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}