Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Sunday 21 July 2013

Portlets in spring

Hi,

The article will shows you to integrate spring framework with portal frameworks (e.g. Liferay).No matter for which portal (Liferay or JBoss or Weblogic) you are integrating the spring and which IDE you are using but it should have following components.

  • portlet.xml  in WEB-INF folder.
  • Web.xml
  • Required libs (described in snap below)
  • springContextConfig.xml in WEB-INF 
  • jdbc.properties


Step-1
 We need to edit Web.xml for ViewRenderServlet Class loading
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">

    <display-name>portletMvcDemo</display-name>

    <servlet>
        <servlet-name>ViewRendererServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ViewRendererServlet</servlet-name>
        <url-pattern>/WEB-INF/servlet/view</url-pattern>
    </servlet-mapping>

    <!-- optional for basic config -->
    <jsp-config>
        <!-- if you are using liferay as portlet server and want to take advantage
            of liferay taglib -->
        <taglib>
            <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
            <taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
        </taglib>
        <!--if you want to use jstl taglib -->
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/core_rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support.
For usage with Portlets, this Servlet is necessary to force the portlet container to convert the PortletRequest to a ServletRequest, which it has to do when including a resource via the PortletRequestDispatcher. This allows for reuse of the entire Servlet-based View support even in a Portlet environment.
The actual mapping of the bridge servlet is configurable in the DispatcherPortlet, via a "viewRendererUrl" property. The default is "/WEB-INF/servlet/view", which is just available for internal resource dispatching.

Step -2
We need to add DispatcherPortlet in Portlet.xml file
DispatcherPortlet is central dispatcher for use within the Portlet MVC framework, e.g. for web UI controllers. Dispatches to registered handlers for processing a portlet request.

We need to also add SpringContext configuration file in init parameter of portlet.xml, which will load context of spring beans.


<?xml version="1.0"?>

<portlet-app
    version="2.0"
    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"
>
    <portlet>
        <portlet-name>eis</portlet-name>
        <display-name>Eis</display-name>
            <!-- this will invoke spring dispatcher portlet -->
            <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
        <init-param>
            <!-- Spring context configuration load -->
            <name>contextConfigLocation</name>
            <value>
                /WEB-INF/springContextConfig.xml
            </value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
            <portlet-mode>EDIT</portlet-mode>
            <portlet-mode>HELP</portlet-mode>
        </supports>
        <resource-bundle>content/Language</resource-bundle>
        <portlet-info>
            <title>Eis</title>
            <short-title>Eis</short-title>
            <keywords>Eis</keywords>
        </portlet-info>
        <security-role-ref>
            <role-name>administrator</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>guest</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>power-user</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>user</role-name>
        </security-role-ref>
    </portlet>
</portlet-app>


Step-3
Now turn for the configuring Spring Context file. Three main parts to configure, rest of all is spring and service layer stuff.
First part is setting PortletModeHandlerMapping for Diffrent portlet modes(VIEW,EDIT,HELP). we need to create handler (Spring MVC abstract controller class) for handling VIEW mode of portlet.

Second part is creating ParameterMappingInterceptor to forward a request parameter from the ActionRequest to the RenderRequest

Third part is configuring PortletModeParameterHandlerMapping, assigning interceptor to it

<?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:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">



 <!-- ########## Hibernate configuration############### -->
 <bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
   <value>/WEB-INF/jdbc.properties</value>
  </property>
 </bean>

 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.default.driverClassName}" />
  <property name="url" value="${jdbc.default.url}" />
  <property name="username" value="${jdbc.default.username}" />
  <property name="password" value="${jdbc.default.password}" />
 </bean>

 <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mappingResources">
   <list>
    <!--<value>/model/employee.hbm.xml</value> -->
   </list>
  </property>
  <property name="hibernateProperties">
   <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
  </property>
 </bean>

 <!-- Transaction Manager -->
 <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!-- ########## services ############### -->
 <!--
 <bean id="emplyoeeService" class="com.hardik4u.practice.eis.service.EmployeeServiceImpl" >
  <property name="employeeDAO" ref="employeeDAO" />
 </bean>
 -->
 <!-- ########## DAO ############### -->
 <!--
 <bean id="employeeDAO" class="com.hardik4u.practice.eis.dao.EmployeeDAOHib" >
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 -->
 <!-- ########## Controller ############### -->

 <bean id="viewHandler" class="com.hardik4u.practice.eis.handler.ViewHandler" />
 <!--<bean id="employeeController" class="com.hardik4u.practice.eis.controller.EmployeeController">
  <property name="commandClass"
   value="com.hardik4u.practice.eis.command.EmployeeCommand" />
  <property name="commandName" value="employeeCommand" />
  <property name="emplyoeeService" ref="emplyoeeService" />
 </bean>
 -->
 <!-- ##########Portlet Controller Configurations############### -->

 <bean id="parameterMappingInterceptor"
  class="org.springframework.web.portlet.handler.ParameterMappingInterceptor" />

 <bean id="portletModeHandlerMapping"
  class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
  <property name="portletModeMap">
   <map>
    <entry key="view" value-ref="viewHandler" />
   </map>
  </property>
 </bean>

 <bean
  class="org.springframework.web.portlet.handler.PortletModeParameterHandlerMapping">
  <property name="order" value="1" />
  <property name="interceptors">
   <list>
    <ref bean="parameterMappingInterceptor" />
   </list>
  </property>

  <property name="portletModeParameterMap">
   <map>
    <entry key="view"> <!-- 'view' portlet mode -->
     <map>
      <!--<entry key="addpage" value-ref="employeeController"/>
      <entry key="list" value-ref="employeeController"/>
      <entry key="editpage" value-ref="employeeController"/>
      <entry key="save" value-ref="employeeController"/>
      <entry key="delete" value-ref="employeeController"/> -->
     </map>
    </entry>
   </map>
  </property>
 </bean>

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="prefix" value="/html/jsp/" />
  <property name="suffix" value=".jsp" />
  <property name="viewClass">
   <value>org.springframework.web.servlet.view.JstlView</value>
  </property>
 </bean>

</beans>
Step-4

Creating viewHandler class for handling view mode first time (com.hardik4u.practice.eis.handler.ViewHandler)
package com.hardik4u.practice.eis.handler;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;

public class ViewHandler extends AbstractController{
   
    public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("view");
        System.out.println("INside View Handler>>>>>>>>");
        mav.addObject("message", "Check it out!");

        return mav;
    }
   
    @Override
    protected void handleActionRequestInternal(ActionRequest request,
            ActionResponse response) throws Exception {
        // TODO Auto-generated method stub
        super.handleActionRequestInternal(request, response);
    }
}
 
Here configuration part is almost complete only you need to set up /WEB-INF/jdbc.properties and need to copy required lib in /WEB-INF/lib folder.

required lib files snap as below.



Your configuration part is done. now you can create Spring controller and manage it via portletModeParameterMap in context configuration files.

Please let me know in case if any suggestion. All suggestions are acceptable.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...