Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating a struts action in Liferay

    • 0
    • 2
    • 1
    • 3
    • 0
    • 0
    • 0
    • 0
    • 520
    Comment on it

    We can create custom Struts action in Liferay by using Hooks, these type of Hook is called as Liferay Struts Action Hook.

    Follow the below steps to create a Struts Action:

    1. Create Liferay Plugin Project of type Hook.

    1. 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>
        <portal-properties>portal.properties</portal-properties> 
        <custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>
        <struts-action>
            <struts-action-path>/portal/createActionExample</struts-action-path>
            <struts-action-impl>com.evon.action.CustomAction</struts-action-impl> 
        </struts-action>  
    </hook>
    

    1. Create demo.jsp in the portal directory. We can add this jsp by JSP Hook in docroot/META-INF/custom_jsps/html/portal directory:

    <h3>Welcome! you are redirected here from Custom Struts Action class CustomAction</h3>
    

    1. Now create CustomAction class for Struts action as below. CustomAction class extends BaseStrutsAction but not BaseStrutsPortletAction. By this way we add action class in portal level.

    package com.evon.action;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.liferay.portal.kernel.struts.BaseStrutsAction;
    
    public class CustomAction extends BaseStrutsAction{
    
        @Override
        public String execute(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
    
            return "/portal/demo.jsp";
        }
    
    }
    

    1. If you want to make struts action path to be public then create portal.properties in docroot/WEB-INF/src and write the below line in portal.properties

    auth.public.paths=/portal/createActionExample
    

    1. Deploy the Hook and access the http://localhost:8080/c/portal/createActionExample URL. It will display demo.jsp page and you will see the message that we have defined in that jsp.

    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: