Tuesday 2 September 2014

Spring MVC portlet using maven

Create Maven base Liferay portlet
Please following the below link to create a Liferay portlet using maven command prompt.
http://wellofjava.blogspot.com/2014/08/maven-portlet-development-for-command.html

Define Maven dependencies for Sping MVC portlet
Now you will need to add following dependencies in Maven file to make spring portlet.


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc-portlet</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

Add spring.version as property. You can choose version as per your use.
               
<properties>
    <spring.version>3.1.0.RELEASE</spring.version>
</properties>


Define Portlet for Spring mvc
In portlet.xml file, you need to define portlet class which will act as controller for portlet. When you create portlet in Liferay using SDK or using maven, You will find following line in portlet.xml.

<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

You need to change this portlet class to use spring mvc framework.


<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>

When a DispatcherServlet or DispatcherPortlet is set up for use and a request comes in for that specific dispatcher, it starts processing the request. The sections below describe the complete process a request goes through when handled by such a dispatcher, from determining the application context right through to rendering the view.

Create Spring application context file
Now you need to create a spring context file for spring mvc which will contains bean definitions of controller and viewResolver. Create one xml file with name of your portlet or application-context.xml or any other name as per your organization standard.
You need to put this file under WEB-INF. Either you can put this file in your directory under WEB-INF e.g. WEB-INF/spring.
Add the following content in 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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>

Add following lines within <beans> tag to enable spring annotations.  

<context:annotation-config />
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />


Pointing Spring application context file in Portlet configuration

Now once you have to create the context file. You need to point this context file through your portlet configuration to make it effective. Add init-param parameter to portlet definition in portlet.xml file. E.g.
 
<init-param>
    <name>contextConfigLocation</name>
    <value>/WEB-INF/spring/application-context.xml</value>
</init-param> 
   
Put viewrenderservlet entry in web.xml file.
It is necessary to load spring render servlet. Because 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.
Add following xml snippet in web.xml

<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-mapping>
    <servlet-name>view-servlet</servlet-name>
    <url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>

Configuring viewResolver
Spring mvc uses view resolver to forward request to JSP. Spring supports many kind of resolvers. Following are the list of all the view resolvers.

  • AbstractCachingViewResolver, 
  • AbstractTemplateViewResolver, 
  • BeanNameViewResolver, 
  • FreeMarkerViewResolver, 
  • InternalResourceViewResolver, 
  • JasperReportsViewResolver, 
  • ResourceBundleViewResolver,
  • UrlBasedViewResolver, 
  • VelocityLayoutViewResolver, 
  • VelocityViewResolver, 
  • XmlViewResolver, 
  • XsltViewResolver
For easy to use purpose you can use InternalResourceViewResolver. This is very easy to use and it can handle JSP. Add following code snippet to add view resolver to your portlet.

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1" />
</bean>

In prefix property we have to give the folder path where we kept all JSP files.
In suffix property we have to give as ".jsp".
Create a jsp file in /WEB-INF/jsp directory.

Create Portlet Controller
Now we need to implement controller to handle tasks for portlet. In controller each method is invoked by Request Parameter mapping.
We have used different annotation to implement controller.


@Controller(value = " SpringMVCTestController ")
@RequestMapping("VIEW")
public class SpringMVCTestController {
    @RenderMapping
    public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){
        return "defaultRender";
    }
}


Define controller
Now once your controller is ready, you need to define this controller as bean in spring configuration file.


<bean class="com.myowncompany.test.springmvc.controller.MyFirstSpringMVCTestController" />

0 comments:

Post a Comment