Wednesday 14 August 2013

Spring MVC Portlet

create Liferay MVC portlet
Then we will do changes to migrate it into Spring MVC Portlet.
Open portlet.xml file

<portlet>
  <portlet-name>first-spring-mvc</portlet-name>
  <display-name>First Spring Mvc</display-name>
  <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>
  <init-param>
   <name>view-jsp</name>
   <value>/view.jsp</value>
  </init-param>
  <expiration-cache>0</expiration-cache>
  <supports>
   <mime-type>text/html</mime-type>
  </supports>
  <portlet-info>
   <title>First Spring Mvc</title>
   <short-title>First Spring Mvc</short-title>
   <keywords>First Spring Mvc</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>



 Now our portlet class will be DispatcherPortlet (provided by Spring Framework). replace
com.liferay.util.bridges.mvc.MVCPortlet with org.springframework.web.portlet.DispatcherPortlet


define the Spring application context file like <<PORTLET_NAME>>-portlet.xml  under docroot/WEB-INF



<?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">
 <context:annotation-config />
 <bean
  class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
</beans>




POINTING SPRING APPLICATION CONTEXT FILE TO PORTLET CLASS.

We had defined DisptacherPortlet class as portlet class in portlet.xml file. Its our central(Front) controller. We have to tell it where we have defined our application context file.

We will point it through init param in portlet.xml file. param name will be "contextConfigLocation" and its value will the location of spring application context file.

After adding this, the portlet.xml file will look like below snippet (I am only showing the code which I have changed)


<portlet-name>first-spring-mvc</portlet-name>
<display-name>First Spring Mvc</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
 <init-param>
  <name>contextConfigLocation</name>
  <value>/WEB-INF/first-spring-mvc-portlet.xml</value>
 </init-param>
 <init-param>
  <name>view-jsp</name>
  <value>/view.jsp</value>
 </init-param>


 PUTTING SPRING JARS INTO CLASS PATH.

 liferay-plugin-package.properties file

    spring-web-servlet.jar
    spring-web-portlet.jar
    spring-web.jar
    spring-transaction.jar
    spring-jdbc.jar
    spring-expression.jar
    spring-core.jar
    spring-context.jar
    spring-beans.jar
    spring-asm.jar
    spring-aop.jar
    commons-beanutils.jar
    commons-collections.jar
    commons-fileupload.jar
    commons-io.jar
    commons-lang.jar
    jstl-api.jar
    spring-context-support.jar
    jstl-impl.jar



PUTTING ViewRenderServlet ENTRY IN web.xml FILE.

<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 VIEW
 Spring application context file (first-spring-mvc-portlet.xml ).

 <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>



CREATE JSP

Now we will create JSP. Create folder jsp under /WEB-INF folder and create one jsp file called defaultRender.jsp and simple write one line in it like "<h1>This is Default Render Jsp</h1>"


CREATE REQUEST HANDLER (CONTOLER)


package com.myowncompany.test.springmvc.controller;
 
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
 
@Controller(value = "MyFirstSpringMVCTestController")
@RequestMapping("VIEW")
public class MyFirstSpringMVCTestController {
 
 
}








DEFINE CONTROLLER(REQUEST HANDLER) IN SPRING APPLICATION CONTEXT





<?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: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/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">
         
 <context:annotation-config />
 <bean
  class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
 
 <bean class="com.myowncompany.test.springmvc.controller.MyFirstSpringMVCTestController" />
 
 <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>
</beans>



refer


http://www.opensource-techblog.com/2012/09/spring-mvc-portlet-in-liferay.html

No comments:

Post a Comment