Liferay DXP Struts action HOOK


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 DXP Struct actions override tutorial:

  1. 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 templateLiferay 7 Struts action hook example
  2. 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
  3. Delete the Activator class that created and remove the Bundle-Activator reference from bnd.bnd file
  4. bnd.bnd file will be :
    1. Bundle-Name: hook-actions
      Bundle-SymbolicName: org.javasavvy.hook.actions
      Bundle-Version: 1.0.0
  5. update build.gradle file and refresh the Gradle dependencies. Right click on the module project -> Gradle -> Refresh Gradle Project to load the dependencies
    1. 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"
      }
  6. Create Class with “CustomTermsOfUseAction”
  7. For Struts actions,class need to extend BaseStrutsAction and @Component configuraiton would be:
    1. @Component(
       immediate=true,
       property={
       "path={struts-action-path}"
       },
       service = StrutsAction.class
      )
  8. CustomTermsOfUseAction.java would be:
    1. 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);
       }
      
      }
  9. 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]
  10. You add your own business logic there while accepting terms and conditions.

 

Click here to download code

 

5 thoughts on “Liferay DXP Struts action HOOK”
  1. 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

  2. Hi,

    I have also tried overriding /portal/verify_email_address (VerifyEmailAddressAction) using the method you showed here. Everything get built and deployed correctly (meaning no issues/exceptions occurred), however once I actually call the URL I only get a blank screen. What is more weird is that I have put a break point in my overridden action class and in debugging it was never caught.

    Would you maybe be able to try this in Liferay 7.2 and verify this process?

    Thank you.

  3. Hi,
    From Liferay 7.3 BaseStrutsAction deprecated.
    How to achieve this in Liferay 7.3?

    I tried like below – when am click on Terms of use “I Agree” button my logic is executed – but getting blank screen or not going to landing page.

    @Component(
    immediate=true,
    property={
    “path=/portal/update_terms_of_use”
    },
    service = StrutsAction.class
    )
    public class CustomTermsOfUseAction implements StrutsAction {

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    System.out.println(“Calling Custom Termsof Use Action”);
    //you logic goes here
    return null;
    }

    }

Leave a Reply