Friday, July 30, 2010

Spring MVC portlet with annotations

Java portlet framework is awesome for portal developers. Different portal frameworks are there which supports portlet development in their environment. They also support spring portlet. Spring's portlet support is exposed through Spring MVC interface. Spring provides MVC based portlet development which is so easy and neatly architectured.

Spring provides class org.springframework.web.portlet.DispatcherPortlet which is to be difined in portlet.xml.
  <portlet>  
     <description xml:lang="en">Portlet Description</description>  
     <portlet-name>portlet-name</portlet-name>  
     <display-name xml:lang="en">Portlet Display Name</display-name>  
     <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>  
     <init-param>  
       <name>contextConfigLocation</name>  
       <value>/WEB-INF/appContext.xml</value>  
     </init-param>  
     <expiration-cache>0</expiration-cache>  
     <supports>  
       <mime-type>text/html</mime-type>  
       <portlet-mode>VIEW</portlet-mode>  
     </supports>  
     <portlet-info>  
       <title>Portlet Name</title>  
       <short-title>Portlet Name</short-title>  
       <keywords>Spring Portlet MVC</keywords>  
     </portlet-info>  
   </portlet>   

The init param contextConfigLocation defines the application context XML file to be loaded for this portlet. This file has the spring bean definition for classes implementing Controller interface OR annotated classes with @Controller.

Annotated Controller class is as follows,
 @Controller  
 @RequestMapping  
 public class MyController {  
   
     private static final Logger _LOG = Logger.getLogger(MyController.class);  
       
     //Default render method will call this method...  
     @SuppressWarnings("unchecked")  
     @RequestMapping({"VIEW","/demoportlet/jsp2.do"})  
     public Object defaultRender(Model model, PortletRequest request,RenderResponse response) {  
         response.setContentType("text/html; charset=UTF-8");  
         String action = request.getParameter("action");  
         if("action1".equals(action)) {  
              ...  
             return "demoportlet/jsp1";  
         } else {              
              ...  
             return "demoportlet/jsp2";  
         }  
     }  
       
     // Direct request mapping based on action parameter value = 'someAction'  
     @RequestMapping(params = "action=someAction")   
     public Object actionOne(RenderRequest actionRequest, RenderResponse actionResponse) throws Exception {  
         actionResponse.setContentType("text/html; charset=UTF-8");  
          ...          
         return "demoportlet/jsp2";  
     }  
 }  
   

In web.xml we need to define a servlet org.springframework.web.servlet.DispatcherServlet and org.springframework.web.servlet.ViewRendererServlet for JSP resolving as follows,

<servlet>  
     <servlet-name>view-servlet</servlet-name>  
     <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>  
     <load-on-startup>1</load-on-startup>  
  </servlet>  
    
  <servlet>  
  <servlet-name>normal</servlet-name>  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  <load-on-startup>1</load-on-startup>  
  </servlet>   
   
  <servlet-mapping>  
  <servlet-name>normal</servlet-name>  
  <url-pattern>*.do</url-pattern>  
  </servlet-mapping>   
    
  <servlet-mapping>  
     <servlet-name>view-servlet</servlet-name>  
      <url-pattern>/WEB-INF/servlet/view</url-pattern>  
  </servlet-mapping>  

In order for DispatcherPortlet rendering to work, you must declare an instance of the ViewRendererServlet .

In the Portlet MVC framework, each DispatcherPortlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the portlet-specific scope, and new scope-specific beans can be defined local to a given portlet instance.

The DispatcherServlet will look for normal-servlet.xml as we declared its name as 'normal'. This file will contain the definitions for beans which are needed to handle the portlet scanning, annotation handling etc... So first of all, the request will come to DispatcherServlet as its Spring MVC, and then it will scan for portlet controllers using spring's <context:component-scan> tag.

We also need to define the context param contextConfigLocation in web.xml which is loaded by DispatcherServlet on server startup. This will have all the beans required by portlets.

 <context-param>  
  <description>Spring Context XML location</description>  
  <param-name>contextConfigLocation</param-name>  
  <param-value>classpath:conf/spring/global.xml</param-value>  
  </context-param>  

The normal-servlet.xml will look like follows, where we need to define beans for annotation handling.
 <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"  
     xmlns:util="http://www.springframework.org/schema/util"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
           
     <context:annotation-config />          
   
     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">  
       <property name="cookieName" value="lang"/>  
       <!-- in seconds. If set to -1, the cookie is not persisted (deleted when browser shuts down) -->  
       <property name="cookieMaxAge" value="100000"/>  
     </bean>  
   
     <bean id="localeChangeInterceptor"  
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">  
       <property name="paramName" value="locale"/>  
     </bean>  
       
     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
         <property name="interceptors">  
             <list>  
                 <ref bean="localeChangeInterceptor"/>  
             </list>  
         </property>  
         <property name="order" value="10" />  
     </bean>    
     
   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
       
     <context:component-scan base-package="com.spring.controller" />      
 </beans>  
   

The <context:component-scan> will finds the controller classes from given package which are annotated with @Controller.

Other than is, we need to enter portlet information in portal framework's configuration files in which we want to deploy and use this portlet. When we add portlet from the portal, the controller methods will get call.

Wednesday, July 28, 2010

Display tag - Opensource JSP paging library...


Paging is important part when we have much data to show at once. We need to do all hectic coding for paging, like keep the total count, then count total pages to be shown, keep total records per page etc... This is nothing new but repeating and it takes bit time. So why not use some lib which gives you all of this with basic tags?

Note from the homepage : "The display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model. The library provides a significant amount of functionality while still being easy to use."

Basic steps to use are:

1. Get the jar file from http://displaytag.sourceforge.net and put it in classpath.

2. In your JSP page write following import for taglib.

 <%@ taglib uri="http://displaytag.sf.net" prefix="display" %>  

3. Check the following code for displaying the list of Objects as table with paging. You can configure different propeties of table as below, also can define CSS classes for diff. tags.


 <display:table name="lstObject" defaultsort="1" pagesize="20" sort="external" partialList="true"   
     size="totalCount" uid="lstObject" id="myObject"  
             export="false" requestURI="<%=someUrl%>" class="data">   
               
             <!-- For detailed understanding for each property, check the link http://displaytag.sourceforge.net/1.2/configuration.html -->  
             <display:setProperty name="paging.banner.placement" value="both" />  
             <display:setProperty name="paging.banner.group_size" value="10" />      
             <display:setProperty name="paging.banner.no_items_found">  
                 <span class="lfloat">No {0} found.</span>  
             </display:setProperty>  
             <display:setProperty name="paging.banner.one_item_found">  
                 <span class="lfloat">One {0} found.</span>  
             </display:setProperty>              
             <display:setProperty name="paging.banner.all_items_found">  
                 <span class="lfloat">{0} {1} found, displaying all {2}.</span>  
             </display:setProperty>  
             <display:setProperty name="paging.banner.some_items_found">  
                 <span class="lfloat">{0} {1} found, displaying {2} to {3}.</span>  
             </display:setProperty>              
             <display:setProperty name="paging.banner.full">  
                 <div class="paging_footer">  
                     <div class="rfloat">  
                         <div class="lfloat" style="padding-top:6px;">[<a href="{1}">First</a> | <a href="{2}">Prev</a>]</div>  
                         <div class="paging_wrap lfloat">                          
                             {0}                          
                         </div>  
                         <div class="rfloat" style="padding-top:6px;">[<a href="{3}">Next</a> | <a href="{4}">Last</a>]</div>  
                     </div>  
                 </div><br>  
             </display:setProperty>  
             <display:setProperty name="paging.banner.first">  
                 <div class="paging_footer">  
                     <div class="rfloat">  
                         <div class="lfloat" style="padding-top:6px;">[First | Prev]</div>  
                         <div class="paging_wrap lfloat">  
                              {0}  
                         </div>  
                         <div class="rfloat" style="padding-top:6px;">[<a href="{3}">Next</a> | <a href="{4}">Last</a>]</div>  
                     </div>  
                 </div><br>  
             </display:setProperty>          
             <display:setProperty name="paging.banner.last">                  
                 <div class="paging_footer">  
                     <div class="rfloat">  
                         <div class="lfloat" style="padding-top:6px;">[<a href="{1}">First</a> | <a href="{2}">Prev</a>]</div>  
                         <div class="paging_wrap lfloat">                          
                             {0}  
                         </div>  
                         <div class="rfloat" style="padding-top:6px;">[Next | Last]</div>  
                     </div>  
                 </div><br>  
             </display:setProperty>  
             <display:setProperty name="paging.banner.onepage" value="" /> <!-- Because dont want to show any value if only one page...-->  
               
             <display:setProperty name="paging.banner.page.selected">  
                 <div class="lfloat">  
                     <a onclick="return false;" class="active" href="javascript:void(0);">{0}</a>  
                 </div>  
             </display:setProperty>                      
             <display:setProperty name="paging.banner.page.link">  
                 <div class="lfloat">  
                     <a href="{1}" title="Goto page {0}">{0}</a>  
                 </div>  
             </display:setProperty>  
               
             <display:setProperty name="paging.banner.page.separator" value=" " />    <!-- Seperator between page numbers ,, i.e this will show "[First | Prev] 1 2 3 [Last | Next]"-->          
             <% MyObject myObject = (MyObject)pageContext.getAttribute("myObject"); %> <!-- Get the object from pageContext, which is writter in 'id' attribute -->  
              <display:column title="My title" class="first">  
              <!-- An y HTMl or jsp scriptlet can be used here... -->  
              </display:column>  
              <display:column property="name" title="2nd Column"/>  
              <display:column title="3rd Column"><%= myObject.getSomeValue() %></display:column>  
              ...               
         </display:table>  

4. The displaytag table can be used as follows. Where,

- pagesize is total records per page

- sort="external" means list given is sorted

- size defined the total records for which paging will be done

- name defines the list object for which the table will be generated

- id means internal object for each row which can be used in JSP scriptlets to do some data manipulations.

 <display:table name="lstObject" defaultsort="1" pagesize="20" sort="external" partialList="true"   
     size="totalCount" uid="lstObject" id="myObject"  
             export="false" requestURI="<%=someUrl%>" class="data">  
             ...  
             </display:table>  

5. You can set any property for display tag as below. You can also write HTML inside the property tag. For detailed understanding for each property this library supports, check the link - http://displaytag.sourceforge.net/1.2/configuration.html

 <display:setProperty name="paging.banner.page.separator" value=" " />