Total Pageviews

Friday, September 3, 2010

Developing Struts Portlet

Before creating a struts portlet, we are supposed to know what is the need of a struts of portlet, if simple JSP portlet is existing.

Why use Struts?

  • Struts implements MVC. Although there are other frameworks that implement MVC, Struts is the most widely used and mature technology.
  • What is MVC? MVC separates the presentation code from the business logic code.
  • Struts provides centralized page-flow management in the form of struts-config.xml. This makes it highly scalable and allows you to modularize the coding process.
  • By using Struts, you will be using a number of best practices that have been built into the framework.

Why use Tiles?

A page layout is typically designed using include statements. If there are 100 JSPs and the header and footer need to be swapped, all 100 JSPs need to be changed. With Tiles, a single template can be used to determine the page layout. Only the template needs to be changed, and all the pages will be updated accordingly.

Flow -
  • A URL or URI is passed to the Controller.
  • The Controller determines what page should be displayed.
So, lets see practically, how to develop it-

STEP 1 :  portlet-ext.xml

<portlet>
 <portlet-name>EXT_5</portlet-name>
  <display-name>Struts Portlet</display-name>
  <portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
  <init-param>
   <name>view-action</name>
   <value>/ext/ApoStrutsPortlet/ApoStrutsPortlet</value>
  </init-param>
  <expiration-cache>0</expiration-cache>
  <supports> 
   <mime-type>text/html</mime-type>
  </supports>
  <resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
  <security-role-ref>
   <role-name>power-user</role-name>
  </security-role-ref>
  <security-role-ref>
   <role-name>user</role-name>
  </security-role-ref>
</portlet>

STEP 2 :  liferay-portlet-ext.xml
<portlet>
 <portlet-name>EXT_5</portlet-name>
 <struts-path>ext/ApoStrutsPortlet</struts-path>
 <use-default-template>false</use-default-template>
</portlet>
STEP 3 : struts-config.xml
<action path="/ext/ApoStrutsPortlet/ApoStrutsPortlet" type="com.ext.portlet.ApoStrutsPortlet.ViewStrutsAction"> 
 <forward name="portlet.ext.ApoStrutsPortlet.ApoStrutsPortlet" path="portlet.ext.ApoStrutsPortlet.ApoStrutsPortlet" />
</action>

STEP 4:  tiles-def.xml
<definition name="portlet.ext.ApoStrutsPortlet.ApoStrutsPortlet" extends="portlet">
 <put name="portlet_content" value="/portlet/ext/ApoStrutsPortlet/ApoStrutsPortlet.jsp" />
</definition>

STEP 5:  init.jsp
<%@ include file="/html/portlet/init.jsp" %>

STEP 6: ApoStrutsPortlet.jsp
This is Apoorv's first struts portlet... 
<%@ include file="/html/portlet/ext/ApoStrutsPortlet/init.jsp" %>

STEP 7: ViewStrutsAction.java

package com.ext.portlet.ApoStrutsPortlet;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
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.kernel.util.Validator;
import com.liferay.portal.struts.PortletAction;

public class ViewStrutsAction extends PortletAction {

 public void processAction(ActionMapping mapping, ActionForm form,
   PortletConfig config, ActionRequest req, ActionResponse res)
   throws Exception {
  System.out.println("inside process action");
 }
 public ActionForward render(ActionMapping map, ActionForm form,
   PortletConfig config, RenderRequest request, RenderResponse response)
   throws Exception {

  System.out.println("something in action class ");
  
  
   return map.findForward("portlet.ext.ApoStrutsPortlet.ApoStrutsPortlet");
 }

}


I didn't anything more than that... So, please try it out... Thanks