liferay Struts Action Hooks


Example

This type of Hook can be used to override core portal (e.g c/portal/login) and portlet struts actions (e.g /login/forgot_password), this actions for Liferay Portal are specified in a struts-config.xml file in its WEB-INF folder.To override an action:

  1. in liferay-hook.xml file of your hook plugin under docroot/WEB-INF, add a struts-action element within the hook element.
  2. Inside struts-action element, add struts-action-path that specifies the action path you’re overriding and struts-action-impl that specifies your custom action class.This looks like:
 <struts-action-path>/login/login</struts-action-path>
    <struts-action-impl>
    com.myhook.action.ExampleStrutsPortletAction
    </struts-action-impl>
</struts-action> 
  1. Create a Struts portlet action class that extends BaseStrutsPortletAction. An example of this class is:
 public class ExampleStrutsPortletAction extends BaseStrutsPortletAction {

        public void processAction(StrutsPortletAction originalStrutsPortletAction,
                PortletConfig portletConfig, ActionRequest actionRequest,
                ActionResponse actionResponse) throws Exception {

            System.out.println("Custom Struts Action");

            originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                    portletConfig, actionRequest, actionResponse);
        }

    public String render(StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, RenderRequest renderRequest,
            RenderResponse renderResponse) throws Exception {

        System.out.println("Custom Struts Action");

        return originalStrutsPortletAction.render(null, portletConfig,
                renderRequest, renderResponse);
    }
}

Calling the method being overridden, like originalStrutsPortletAction.processAction, is not obligatory but a best practice to keep the behavior from the Action unchanged in regards of Liferay Portal. This type of hook can be used to add new Struts Actions also, it's the same as modifying an existing action, in this case liferay-hook.xml would be:

<struts-action>
    <struts-action-path>/my/custom/path</struts-action-path>
    <struts-action-impl>
    com.myhook.action.ExampleStrutsAction
    </struts-action-impl>
</struts-action>