Wednesday, April 17, 2013

Liferay Struts2 Portlet intergrate and example


 Using MVC frame available in liferay is also pretty good, but if that integrate Struts 2 portlet in liferay to create liferay development purposes is also quite simple and easy for the developer. Following is an example for a framework porlet on 2.x

A portlet project structure is as follows::



STEP1:  Add all file .jar of Struts 2.x:

STEP 2: Config file Portlet.xml 

<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 id="liferayStruts">
<portlet-name>liferayStruts</portlet-name>
<display-name>LiferayStruts</display-name>
<portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class>
<!-- The namespace for the actions configured for view mode -->
<init-param>
<name>viewNamespace</name>
<value>/view</value>
</init-param>
<!-- The default action to invoke in view mode. -->
        <init-param>
         <name>defaultViewAction</name>
         <value>index</value>
        </init-param>
<!-- The namespace for the actions configured for edit mode -->
        <init-param>
         <name>editNamespace</name>
         <value>/edit</value>
        </init-param>
<!-- The default action to invoke in edit mode. -->
        <init-param>
         <name>defaultEditAction</name>
         <value>index!input</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>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Liferay Struts</title>
<short-title>LiferayStruts</short-title>
<keywords>LiferayStruts</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: Create file struts.xml

<constant name="struts.devMode" value="true" />
<package namespace="/view" extends="struts-portlet-default" name="view">
<action name="index">
<result>/html/view/index.jsp</result>
</action>
<action class="com.fpt.action.bank.HelloWorldAction" method="execute" name="hello">
<result name="success">/html/view/HelloWorld.jsp</result>
</action>
 </package> 

STEP4: Create files display jsp

- Create folder html in docroot
-  Create folder view in folder html

file init.jsp:

<%@page import="com.liferay.portlet.PortletURLUtil"%>
<%@page import="javax.portlet.PortletMode"%>
<%@page import="javax.portlet.WindowState"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Random" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.liferay.portal.kernel.dao.search.ResultRow" %>
<%@ page import="com.liferay.portal.kernel.dao.search.SearchEntry" %>
<%@ page import="com.liferay.portal.kernel.util.DateUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ListUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.WebKeys" %>
<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="javax.portlet.ActionRequest" %>
<%@ page import="javax.portlet.PortletRequest" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="javax.portlet.PortletSession" %>
<%@ page import="com.liferay.portlet.PortletPreferencesFactoryUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ListUtil" %>
<%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
<%@ page import="com.liferay.portal.kernel.dao.search.*" %>
<%@ page import="com.liferay.portal.security.permission.ActionKeys"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="sj" uri="/struts-jquery-tags" %>
<%@taglib prefix="sjg" uri="/struts-jquery-grid-tags" %>
<liferay-theme:defineObjects />
<portlet:defineObjects/>

file index.jsp:

<%@include file="/html/init.jsp" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<h1>Xin chao Struts 2!</h1>
<p><a href="<s:url action='hello'></s:url>">Hello World</a></p>
<p><a href="<s:url action='viewBookmarks'></s:url>">Bookmarks</a></p>

file HelloWorld.jsp

<%@include file="/html/init.jsp" %>
<%@taglib prefix="s" uri="/struts-tags" %>

<h2>
<s:property value="messageStore.message"/>
</h2>




STEP 5: Create file Action
File Action inherit from Class of lib xwork: DefaultActionSupport

Create class: HelloWorldAction.java in package com.fpt.action.bank;

HelloWorldAction.java

public class HelloWorldAction extends DefaultActionSupport{

/**
* Author: trunglv
*/
private static final long serialVersionUID = 1L;
private MessageStore messageStore;
public String execute()throws Exception{
messageStore = new MessageStore() ;
return SUCCESS;
}
public MessageStore getMessageStore() {
return messageStore;
}
public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
}

Create more class:

MessageStore.java

public class MessageStore {
private String message;
public static String BOOKMARK_ADDED_SUCCESSFUL = "The bookmark has been added successfully.";
public static String BOOKMARK_ADDED_FAILED = "The bookmark was not added!";
public static String BOOKMARK_REMOVED_SUCCESSFUL = "The bookmark has been deleted.";
public static String BOOKMARK_REMOVED_FAILED = "The bookmark was not deleted!";
public static String BOOKMARK_RETRIEVE_FAILED = "Could not retrieve bookmarks from the database.";
public MessageStore(){
//setMessage("Xin chao Ban Trung");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}




STEP 6: Register portlet on liferay

After build project success, we are run liferay and registration portlet just make up!




Have a nice day