Welcome to our “How To?” series of post. In this series, we are talking about different development concept in Liferay DXP/7. In this post, I am going to talk about Struts Action Hook.
Liferay offers following list of hooks to override default behaviour of out-of-the-box features,
Many Liferay portlets are implemented using Struts. If you want to change the behaviour of existing portlet you certainly need a mechanism to override its action class. Struts Action hook is used to override existing struts action. Struts action hook help you to achieve it.
Here I assume that you have already read previous posts and installed JDK1.8, Liferay 7/DXP and IDE. These are the prerequisite to proceed with implement Struts action hook.
Now let’s understand the steps to implement struts action hook.
To create a Liferay Workspace in IDE follow this:
Select File → New → Liferay Workspace Project Upon creation of the workspace, the structure should look like below:
You need to create module project for Struts Action hook. Following are the steps to create module,
Click on File menu → Click New → Select Liferay Module Project and provide project name as StrutsActionHook and click next. See following screenshot for your reference,
Provide appropriate class name and package name for the class. For this example, I am providing class name as UpdateEmailStrutsAction.
After you finish the wizard, you should see project structure in eclipse as follow,
You successfully created project structure. Now it’s time to implement business logic to update email address. Implement @Component as follow in class,
Ensure that your class extends BaseStrutsAction class. To implement business logic override, execute() method as follow,
Congratulation! You successfully implemented struts hook. Now let’s quickly understand what you exactly implemented.
In @Component, you configured struts action i.e. ‘/portal/update_email_address’ to instruct container that when user trigger action to update email, please execute business logic mentioned in my class.
In case you want to override different action then mentioned in this example, You can see ‘struts-config.xml’ file to know all out-of-the-box struts actions. You can find this file on following path in your IDE.
tomcat server → liferay-ce-portal-7.0-ga3 → tomcat-8.0.32 → webapps → ROOT → WEB-INF → struts-config.xml.
I hope this post will help you to implement struts hook easily in Liferay DXP. In my next post, I will share ‘How to?’ steps for different implementation in DXP, till then keep learning!