Override Struts Action by Hook in Liferay


Today we will discuss about Struts Action hook.By using Struts Action hook we can achieve two things:-
1)Overriding existing struts Action
2)Add new Struts Action

Today we will discuss about Overriding existing struts Action. Every time you login a Struts Action is called.Struts use a file called struts-config.xml you can find this file inside WEB-INF .

For overriding Struts Action your custom class must extend BaseStrutsPortletAction which implements  StrutsPortletAction which  contain processAction().
So Lets start step by step:-





Step 1:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.
You can see snapshot in my previous article (Step 1)   Portal Properties Hook in Liferay.

Step 2:-Create Action Path
Open Struts-config.xml (\webapps\ROOT\WEB-INF) and search for login action you will find entries as:-

struts-config.xml
<action path="/login/login" type="com.liferay.portlet.login.action.LoginAction">
<forward name="portlet.login.login" path="portlet.login.login" />
<forward name="portlet.login.login_redirect" path="portlet.login.login_redirect" />
</action>

As you can see path "/login/login" goes to "LoginAction" Class. We change this class to our Custom class.So just provide those entries in liferay-hook.xml as:-


liferay-hook.xml
<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
<hook>
<struts-action>
<struts-action-path>/login/login</struts-action-path>
<struts-action-impl>com.test.MyCustomAction</struts-action-impl>
</struts-action>
</hook>

Step 3:-Create Action Class
Inside "docroot/WEB-INF/src" create package com.test and create a class with name MyCustomAction as:

MyCustomAction.java
package com.test;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
import com.liferay.portal.kernel.struts.StrutsPortletAction;
public class MyCustomAction extends BaseStrutsPortletAction{
public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse) throws Exception
{
System.out.println("My Custom Process Action Method is Called");
originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest,actionResponse);
}
public String render(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
System.out.println("My Custom Render Method is Called");
return originalStrutsPortletAction.render(null, portletConfig, renderRequest, renderResponse);
}
}

Explanation:-
Here we create our processAction() and inside this method we call the original processAction() so that flow of liferay not break.In place of SOP you can write your own logic.Similarly for render().

Step 4:-Check Output
Deploy your hook and hit localhost:8080 and then press SignIn button on the top right.Your eclipse console shows:-

My Custom Render Method is Called

Then fill the values in login form and press Sign In



Your eclipse console shows:-

My Custom Process Action Method is Called

Project Structure




You can Download Source code from Override Struts Action Hook in Liferay





Hope this will Help....