Thursday, July 12, 2012

Liferay Hook 5:To override and add Struts Actions:


-      Hook : To override and add Struts Actions:
-      This is new feature recently introduced.
-      It adds new struts action and override existing one.
-       
-      There are two interfaces
-       
-      1) com.liferay.portal.kernel.struts.StrutsAction
-      2) com.liferay.portal.kernel.struts.StrutsPortletAction
-      .
-      StrutsAction : It is used for regular portal struts actions like /c/portal/update_password.
-      StrutsPortletAction : It is used for action within portlet.
-       
-      Steps :
-      We are going to use same hook : myfirsthook-hook :

-      Edit liferay-hook.xml :

xml version="1.0"?>
DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.1.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_1_0.dtd">
<hook>
       <portal-properties>portal.propertiesportal-properties>
       <custom-jsp-dir>/META-INF/custom_jspscustom-jsp-dir>
       <struts-action>
              <struts-action-path>/portal/mynamestruts-action-path>
              <struts-action-impl>
com.liferay.custom.struts.actions.CustomStrutsAction
struts-action-impl>
       struts-action>
       <struts-action>
              <struts-action-path>/message_boards/viewstruts-action-path>
              <struts-action-impl>
com.liferay.custom.struts.actions.CutomStrutsPortletAction
struts-action-impl>
       struts-action>
hook>


-      Create struts action classes as follows : CustomStrutsAction.java
 
package com.liferay.custom.struts.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liferay.portal.kernel.struts.BaseStrutsAction;
import com.liferay.portal.kernel.util.ParamUtil;

public class CustomStrutsAction extends BaseStrutsAction {

       @Override
public String execute(HttpServletRequest request,
              HttpServletResponse response) throws Exception {
       System.out.println("____CustomStrutsAction   execute");
       String name = ParamUtil.get(request, "name","Darshan");
       request.setAttribute("name", name);
return "/portal/myname.jsp";
       }
}


Second struts Action class to wrap View Action of message board portlet

package com.liferay.custom.struts.actions;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
import com.liferay.portal.kernel.struts.StrutsPortletAction;

public class CutomStrutsPortletAction extends BaseStrutsPortletAction {

       @Override
public void processAction(StrutsPortletAction originalStrutsPortletAction,
       PortletConfig portletConfig, ActionRequest actionRequest,
       ActionResponse actionResponse) throws Exception {

System.out.println("____CutomStrutsPortletAction processAction");
             
originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest,actionResponse);
       }

       @Override
public String render(StrutsPortletAction originalStrutsPortletAction,
       PortletConfig portletConfig, RenderRequest renderRequest,
       RenderResponse renderResponse) throws Exception {             System.out.println("_____CutomStrutsPortletAction   render");
return originalStrutsPortletAction.render(originalStrutsPortletAction, portletConfig, renderRequest,renderResponse);
       }

       @Override
public void serveResource(StrutsPortletAction originalStrutsPortletAction,
       PortletConfig portletConfig, ResourceRequest resourceRequest,
       ResourceResponse resourceResponse) throws Exception {
             
       System.out.println("__CutomStrutsPortletAction   serveResource");
originalStrutsPortletAction.serveResource(originalStrutsPortletAction, portletConfig,resourceRequest, resourceResponse);
       }

}

-      Create a jsp myname.jsp in 
       myfirsthook-hook\docroot\META-INF\custom_jsps\html\portal\ folder.
-           myname.jsp

 
Hellloooooooooooooo

<%=request.getAttribute("name")%>

-      Finally modify portal.properties:

-      auth.public.paths=/portal/myname

-      Deploy Hook and go to http://localhost:8080/c/portal/myname

-      Screen Shot :
        



 Other Many Type of Hooks are :

-                    Hooks : Servlet Filter Hook

 






1 comment:

Anonymous said...

Hi, is there any example to get parameters passed via ajax inside serve resource. I have added some extra fields on user account page and upon keypress on those field, an ajax call has to be invoked. My call goes inside serve resource but the parameters passed appear to be null. Thanks

Junit 5 Architecture

Modules in JUnit 5 : •         junit-jupiter-api: This module defines the API that you need to write your tests. •         ...