Thursday 25 July 2013

Basic Spring Portlet Example

Introduction:

A portlet is a servlet-like web component that can process requests and generate responses
dynamically. The content generated by a portlet is usually an HTML fragment that is aggregated into a portal page. Portlets need to be managed by a portlet container.

The Java Portlet specification[JSR-286] defines the contract between a portlet and a portlet container to ensure interoperability between different portal servers.

Spring 3 supports portlet development.

Pre-requisite:

Reader should have knowledge of basic Spring Web MVC Framework and basic Portlet specification JSR286.

Creating Spring Portlet:

We will have annotation base configuration for Controller.

Portlet contains portlet deployment descriptor portlet.xml where we have to define portlet class, which will be front controller for each request.

 <?xml version="1.0"?>  
 <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">  
   <portlet>  
     <portlet-name>test</portlet-name>  
     <display-name>test</display-name>  
     <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>  
     <expiration-cache>0</expiration-cache>      
     <supports>  
       <mime-type>text/html</mime-type>  
       <portlet-mode>view</portlet-mode>  
     </supports>  
     <portlet-info>  
       <title>test</title>  
       <short-title>test</short-title>  
       <keywords>test</keywords>  
     </portlet-info>  
   </portlet>  
 </portlet-app>  


By default, DispatcherPortlet loads its configuration file from the root of WEB-INF by joining the
portlet name with -portlet.xml as the file name (e.g., test-portlet.xml).
You can override its location with the contextConfigLocation parameter.
test-portlet.xml will look like:

 <?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:context="http://www.springframework.org/schema/context"  
   xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
   <!-- THIS DEFINES WHERE TO SCAN FOR ANNOTED BEANS -->  
   <context:component-scan base-package="com.test" />  
   <!-DefaultAnnotationHandlerMapping maps request to class and/or methods that are annotated with @RequestMapping -->  
   <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
   <!--VIEWRESOLWER -->  
   <bean  
     class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
     <property name="viewClass"  
       value="org.springframework.web.servlet.view.JstlView" />  
     <property name="prefix" value="/WEB-INF/view/" />  
     <property name="suffix" value=".jsp" />  
   </bean>  
 </beans>  

In order for Spring Portlet MVC to reuse the view technologies of Spring Web MVC, you have to
configure the bridge servlet ViewRendererServlet in the portlet application’s web deployment
descriptor. This servlet converts portlet requests and responses into servlet requests and responses so
that you can render Spring Web MVC’s servlet-based views for portlets

web.xml

 <?xml version="1.0"?>  
 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
      <servlet>  
           <servlet-name>view</servlet-name>  
           <servlet-class>  
                org.springframework.web.servlet.ViewRendererServlet  
                </servlet-class>  
      </servlet>  
      <servlet-mapping>  
           <servlet-name>view</servlet-name>  
           <url-pattern>/WEB-INF/servlet/view</url-pattern>  
      </servlet-mapping>  
 </web-app>  


Let's see how our controller would look like :

 package com.test;  
 import javax.portlet.ActionRequest;  
 import javax.portlet.ActionResponse;  
 import javax.portlet.RenderRequest;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestParam;  
 @Controller  
 @RequestMapping("VIEW") //DEFINE MODE  
 public class PController{  
     /* PUBLIC METHOD RETURNING STRING AND ANNOTED WITH REQUESTMAPPING IS CONSIDERED AS RENDER METHOD */  
     @RequestMapping  
     public String render(RenderRequest renderRequest){  
     // Rendering view based on attribute set in some action method 
       if(renderRequest.getAttribute("view")==null)  
         return "view";  
       else  
         return renderRequest.getAttribute("view").toString();  
     }  
     /*THIS WILL MAP TO ACTION METHOD FOR ACTION URL HAVING action PARAMETER SET AS submit*/   
     @RequestMapping(params={"action=submit"})  
     public void submit(){  
         /*  
           SOME STUFF  
         */  
         
     }  
     /*/*THIS WILL MAP TO ANY OTHER ACTION METHOD FOR ACTION URL NOT HAVING action PARAMETER SET AS submit */  
     @RequestMapping  
     public void submit1(@RequestParam String name,ActionRequest actionRequest,ActionResponse actionResponse ){  
         //@RequestParam String name makes request parameter named as name available in it.  
        
         /*   
           SOME STUFF  
         */  
         actionRequest.setAttribute("view", "view1");  
     }  
 }  

view.jsp

 <%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>  
 <portlet:actionURL var="formAction">  
   <portlet:param name="action" value="submit"/>  
 </portlet:actionURL>  
    <form action="${formAction}" method="post">  
      <input name="name" type="text"/>  
      <input name"Submit" type="submit"/>  
    </form>  

Hope this exercise will help you creating simple Spring based Portlet Application.
Example Code [maven based portlet] at Basic Spring Portlet .
More on Spring portlet would be covered in upcoming blogs.

Regards.

No comments:

Post a Comment