Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to override existing Struts Action by Struts Action Hook?

    • 0
    • 3
    • 1
    • 3
    • 0
    • 0
    • 0
    • 0
    • 716
    Comment on it

    We can modify Liferay JSP, services, properties by using Hooks. One of the main use of Hook is we can override existing struts actionand also we can add custom struts action, these type of Hook is called as Liferay Struts Action Hook.

    I'm taking here example of struts action "/login/login". Whenever we login in Liferay "com.liferay.portlet.login.action.LoginAction" is the action class that invokes. we're going to change this struts action by Struts Action Hook.

    Follow the below steps to perform the change of Struts Action:

    1. Create Liferay Plugin Project of type Hook.
    2. Write the below code in liferay-hook.xml
    <?xml version="1.0"?>
    <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
    
    <hook>
    <struts-action>
    <struts-action-path>/login/login</struts-action-path>
    <struts-action-impl>com.evon.action.CustomLoginAction</struts-action-impl>
    </struts-action>
    </hook>
    
    1. CustomLoginAction.java
     package com.evon.action;
    
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletConfig;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    
    import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
    import com.liferay.portal.kernel.struts.StrutsPortletAction;
    
    public class CustomLoginAction extends BaseStrutsPortletAction{
    
        @Override
        public void processAction(StrutsPortletAction originalStrutsPortletAction,
                PortletConfig portletConfig, ActionRequest actionRequest,
                ActionResponse actionResponse) throws Exception {
            /**
             * This is the custom process action
             * Once you try to login this method will be invoked
             * We can write our own logic here
             * Invoke the original struts action at the end
             */
    
            System.out.println("in Custom login........................");
    
            originalStrutsPortletAction.processAction(
                    originalStrutsPortletAction, portletConfig, actionRequest,
                    actionResponse);
        }
    
        public String render(
                StrutsPortletAction originalStrutsPortletAction,
                PortletConfig portletConfig, RenderRequest renderRequest,
                RenderResponse renderResponse)
            throws Exception {
    
            return originalStrutsPortletAction.render(
                null, portletConfig, renderRequest, renderResponse);
    
        }
    }
    1. Deploy the Hook and try to login, this will invoke method of CustomLoginAction class.

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: