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:
- Create Liferay Plugin Project of type Hook.
- 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>
- 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>
- 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";
}
}
- 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
- 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)