21 October, 2011

Struts portlet

Here are steps to create a struts portlet using Liferay plugins sdk:


1) Change portlet.xml which would have below content:

<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
<init-param>
<name>view-action</name>
<value>/struts/plugin/view</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>


2) Change Web.xml which would have below content 

(create new web.xml under WEB-INF dir, if it doesn't exists):

<web-app>
<listener>
<listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>PortletActionServlet</servlet-name>
<servlet-class>com.liferay.portal.struts.PortletActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PortletActionServlet</servlet-name>
<url-pattern>/portlet_action/*</url-pattern>
</servlet-mapping>
</web-app>


3) Create a file tiles-defs.xml under WEB-INF which would have below content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name="portlet.xyz.struts.view" path="/struts/jsp/view.jsp"></definition>
</tiles-definitions>

 

4) Create a file struts-config.xml under WEB-INF having below content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action path="/struts/plugin/view" type="xyz.portal.ViewStrutsAction" >
<forward name="portlet.xyz.struts.view" path="portlet.xyz.struts.view" />
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"></controller>
<message-resources parameter="content.test.Language"></message-resources>
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"></set-property>
<set-property property="moduleAware" value="true"></set-property>
<set-property property="definitions-parser-validate" value="true"></set-property>
</plug-in>
</struts-config>

5) check  context.xml wheather its exist in META-INF which would have below content

(create META-INF dir parallel to WEB-INF dir, if it doesn't exists):

<Context>
<Loader loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader" />
</Context>

6) Place your own view.jsp under docroot/html/struts/jsp dir

7) Create cignex/portal/ViewStrutsAction.java under src dir 

(create src directory under WEB-INF, if it doesn't exist).
 
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
 
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
import com.liferay.portal.struts.PortletAction;
 
public class ViewStrutsAction extends PortletAction{
public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse) throws Exception {
System.out.println("SampleStrutsAction render()..");
return mapping.findForward("portlet.xyz.struts.view");
}
}
 

8) Edit build-common.xml under plugins directory :

<path id="plugin.classpath">

<fileset dir="${app.server.lib.global.dir}" includes="*.jar" />

<fileset dir="${app.server.lib.portal.dir}" includes="annotations.jar,commons-logging.jar,log4j.jar,util-bridges.jar,util-java.jar,util-taglib.jar" />

<fileset dir="${project.dir}/lib" includes="*.jar" />

<path refid="plugin-lib.classpath" />

</path>

Note :
If you are using liferay 6 , you might need to include below details :

Create a xml file with the name of your portlet(eg: cignex-portlet.xml) under your tomcat/conf/Catalina/localhost dir.
Add following lines to this file:

<Context>
    <Loader delegate="true" loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader"/>
 </Context>

 
 
Edit your liferay-portlet.xml to have:

<portlet>

<portlet-name>cignex</portlet-name>

<icon>/icon.png</icon>

<struts-path>struts/plugin</struts-path>

<instanceable>true</instanceable>

<header-portlet-css>/css/main.css</header-portlet-css>

<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>

<css-class-wrapper>zyx</css-class-wrapper>

</portlet>


Restart your tomcat and build/deploy your portlet.

 

Add necessary library like portal-impl.jar to your plugins/lib directory or you can also add into eclipse classpath to compile this portlet.

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...