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"?>Step-4
<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>
Creating viewHandler class for handling view mode first time (com.hardik4u.practice.eis.handler.ViewHandler)
package com.hardik4u.practice.eis.handler;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.
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);
}
}
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