Monday 4 January 2016

Liferay Spring portlet

Hi
My post is a reference for me and it is really helpful for me.

In this post I simply creating a simple spring MVC portlet.

First create portlet.
Name of my portlet is myportlet, portlet will added automatically.

Steps for configuration

1) I added my spring related jar files in "liferay-plugin-package.properties" file.
In properties mode

It is difficult to remember each and every jar that we have to added, then just copy paste in source mode in liferay-plugin-package.properties file. Save and close file and refresh your portlet and then open it, it will reload all files if you check in properties mode

portal-dependency-jars=\
    spring-web-portlet.jar,\
    spring-web-servlet.jar,\
    spring-web.jar,\
    spring-context.jar,\
    spring-beans.jar,\
    spring-core.jar,\
    spring-asm.jar,\
    spring-expression.jar,\
    jstl-api.jar


2)  In web.xml file

Add servlet and servlet-mapping tag.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>

</web-app>





3)my portlet.xml file
You have to edit portlet-class and init-param tag.

<?xml version="1.0"?>

<portlet-app 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" version="2.0">

<portlet>
<portlet-name>my-portlet</portlet-name>
<display-name>MyPortlet</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
            <name>contextConfigLocation</name>
            <value>
            /WEB-INF/context/myspring-config.xml
            </value>
        </init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>MyPortlet</title>
<short-title>MyPortlet</short-title>
<keywords>MyPortlet</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>

To understand "DispatcherPortlet" please read this.
Here I provided a path of "myspring-config.xml" file.

4)we have to create "myspring-config.xml" folder inside WEB-INF/context/ folder.

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 
<context:component-scan base-package="com.myspring" />

<bean id="viewResolver"
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" />
</bean>
</beans>

/WEB-INF/jsp/, my all jsp will be inside this folder.


Package of my controller class "com.myspring"


5)Now I am writing my controller class.

package com.myspring


import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.portlet.bind.annotation.RenderMapping;



@Controller
@RequestMapping(value = "VIEW")
public class MyControllerClass{

//if we don't mention any param name with "@RenderMapping" then it is default render method.
//in spring mvc portlet we can create any number of render method as our need.
//So this is our default render method
//and it will always returns to the  "/WEB-INF/jsp/view.jsp"
//whenever hit our controller class either by using render url(if we don't mention any render method //explicitly in our render url)  or by action url
//Note: Render will not execute if we call resourceurl.


@RenderMapping
public String load(RenderRequest request,RenderResponse response)
throws SystemException {
return "view";
}

//This is custom render method and it will execute if direct
//our portlet to execute this specific render method

@RenderMapping(params="action=manualRender")
public String manualRender(RenderRequest request, RenderResponse response){
return "secondview";
}

@ActionMapping(params = "action=redirectSamePage")
public void redirectSamePage(ActionRequest request, ActionResponse response){
System.out.println("this is addContent method");
//Here our default render method will execute after
//execution of action method.
}

@ActionMapping(params = "action=redirectOtherPage")
public void redirectOtherPage(ActionRequest request, ActionResponse response){
response.setRenderParameter("action", "manualRender");
//here we are calling render method "manualRender", so our
//default render method will not execute and hence we will redirect to
//secondview.jsp
}

}

6) I will create one folder /jsp inside WEB-INF folder
Inside /jsp folder I will create two jsp
view.jsp and secondview.jsp


So first view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />
<theme:defineObjects />

<portlet:renderURL var="manualRenderURL" windowState="normal">
<portlet:param name="action" value="manualRender" />
</portlet:renderURL>

<portlet:actionURL var="redirectSamePageURL" name="addUseCaseContent">
<portlet:param name="action" value="redirectSamePage" />
</portlet:actionURL>

<portlet:renderURL var="manualRenderURL" windowState="normal">
<portlet:param name="action" value="manualRender" />
</portlet:renderURL>

<h3>This is view.jsp page for default render method</h3>

<aui:a href="<%= manualRenderURL %>">Click Here</aui:a>


<aui:form action="<%= redirectSamePageURL %>">
<aui:button type="submit" value="Same page" />
</aui:form>

<aui:form action="<%= redirectOtherPageURL %>">
<aui:button type="submit" value="Other page" />
</aui:form>



Note: Here I create one render url in which I mention
name="action" value="manualRender"
We have to create a method with this name that I already created in my controller class.
So this method will execute and inside this render method what string we would return will be our render jsp.
So here I used " secondview.jsp" and hence after clicking that url we will redirect to  secondview.jsp.
Please use a sysout inside manualRender method to check that this method is executing or not.

code of secondview.jsp

<h1>This is secondview.jsp</h1>