Liferay DXP Struts Action Override
Liferay DXP Struts Action Override
Overview
Struts Actions were used in Liferay 6.x as backend controllers for processing the client requests.
They are deprecated already, and being replaced with MVC Commands.
But there are still some struts actions inside Liferay.
This article will explain how to identify and customize them.
Identify Struts Action
Struts Actions are defined in ROOT/WEB-INF/struts-config.xml file, sample:
<action path="/portal/sitemap" type="com.liferay.portal.action.SitemapAction" />
This action is responsible for sitemap creation. Sitemap can be accessed using the /sitemap.xml URL, sample:
Now let’s override this struts action.
Override Struts Action
Create a Liferay module for struts action override:
build.gradle:
dependencies {
compileOnly group: "com.liferay.portal", name: "release.portal.api"
cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
}
bnd.bnd:
Bundle-Name: Liferay Sitemap Struts Action Override
Bundle-SymbolicName: com.liferay.sitemap.struts.action.override
Bundle-Version: 1.0.0
Implement the custom struts action class
Create an OSGi component class for struts action override: LiferaySitemapAction.
Implement the com.liferay.portal.kernel.struts.StrutsAction interface, and implement the execute method.
Specify the “path” property for OSGi component configuration.
Make sure, it matches the “path” property for the original struts action (in struts-config.xml file):
Copy code from the original struts action (com.liferay.portal.action.SitemapAction) and make the required customization.
Sample:
@Component(
immediate = true, property = "path=/portal/sitemap",
service = StrutsAction.class
)
public class LiferaySitemapAction implements StrutsAction {
@Override
public String execute(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
String sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<sitemap><loc>https://liferay.com</loc></sitemap>";
ServletResponseUtil.sendFile(
httpServletRequest, httpServletResponse, null,
sitemap.getBytes(StringPool.UTF8), ContentTypes.TEXT_XML_UTF8);
return null;
}
}
Deploy module, and check the results:
Enjoy 😏
Comments
Post a Comment