Wednesday, April 22, 2015

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

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

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

MyCustomAction.java

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....

Related Post:-

No comments:

Post a Comment

Total Pageviews

Number Of Unique Visitor

Free counters!