Liferay DXP Struts action HOOK
Liferay DXP Struts action HOOK tutorial will drive you about overriding portlets struts action in liferay 7 or liferay DXP. In Liferay 6.2 and lower version, we can directly override all struts actions,
How to override struts actions in Liferay DXP?
- struts actions can be overridden directly with service = StrutsAction.class and only struts action defined in struts-config.xml can only overridden
- portlet actions are overridden through Action Commands
Installations:
- Liferay Eclipse IDE (Download latest version): Download the latest Liferay Eclipse IDE
- Liferay Bundle Tomcat : Download the latest bundle from source forge
- JDK8
- MySQL 7
Liferay DXP Struct actions override tutorial:
- In Liferay Eclipse IDE, Click on File -> New -> Liferay Module Project:
- We will create simple Activator project and will create Struts action component later.
- Select activator from project template
- In this wizard just give package name and some class name. Package name will be used as Bundle-Symbolic name. so give some proper name
- Delete the Activator class that created and remove the Bundle-Activator reference from bnd.bnd file
- bnd.bnd file will be :
-
Bundle-Name: hook-actions Bundle-SymbolicName: org.javasavvy.hook.actions Bundle-Version: 1.0.0
-
- update build.gradle file and refresh the Gradle dependencies. Right click on the module project -> Gradle -> Refresh Gradle Project to load the dependencies
-
dependencies { compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0" compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0" compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0" compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1" compileOnly group: "jstl", name: "jstl", version: "1.2" compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0" compile group: "org.osgi", name:"org.osgi.service.component.annotations", version:"1.3.0" }
-
- Create Class with “CustomTermsOfUseAction”
- For Struts actions,class need to extend BaseStrutsAction and @Component configuraiton would be:
-
@Component( immediate=true, property={ "path={struts-action-path}" }, service = StrutsAction.class )
-
- CustomTermsOfUseAction.java would be:
-
package org.javasavvy.hook.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.service.component.annotations.Component; import com.liferay.portal.kernel.struts.BaseStrutsAction; import com.liferay.portal.kernel.struts.StrutsAction; @Component( immediate=true, property={ "path=/portal/update_terms_of_use" }, service = StrutsAction.class ) public class CustomTermsOfUseAction extends BaseStrutsAction { @Override public String execute(StrutsAction originalStrutsAction, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("Calling Custom Termsof Use Action"); //you logic goes here return super.execute(originalStrutsAction, request, response); } }
-
- Now deploy the bundle and you can see the bundle started message in the logs:[BundleStartStopLogger:35] STARTED org.javasavvy.hook.actions_1.0.0 [516]
- You add your own business logic there while accepting terms and conditions.
Click here to download code
Hi,
I did for UpdatePasswordAction
@Component(immediate = true, property = { “path=/portal/update_password” },
service = StrutsAction.class)
public class CustomUpdatePassword extends BaseStrutsAction {
but it is not working in 7.2 dxp