Let’s say you want to add more content to one of Liferay’s JSPs, but you also want to benefit from any changes made to it as you upgrade Liferay. Rather than overwriting the JSP, you can instead create a new JSP in a hook and include the original JSP in the new JSP.
In this tutorial, you’ll learn how to extend an original Liferay JSP using a hook.
Extending a Liferay JSP
In this demonstration, you’ll customize a hook project to extend a Liferay JSP, while leaving the original JSP untouched. Let’s get started!
Create a hook project, using Liferay IDE or the Plugins SDK, if you’re not already using an existing hook project.
Open your hook’s
liferay-hook.xmlfile from your project’s
docroot/WEB-INFfolder and select the file’s Overview mode tab.
Select the Custom JSPs folder from the outline to bring up the custom JSP options. Select the checkbox Customize Liferay Portal JSPs and create the default custom JSP folder
/META-INF/custom_jspsby clicking the icon that has the three yellow diamonds.
Add to the listing of custom JSPs by clicking the plus icon and specifying the JSP you’d like to extend. Hint: the browse icon on the right-hand side within the custom JSP text field simplifies finding the JSP you want to customize.
Click OK and save the
liferay-hook.xmlfile. Liferay IDE pulls a copy of the Liferay Portal JSP into your project so you can modify it.
Open the JSP file
docroot/META-INF/custom_jsps/[JSP_file's_path]that Liferay IDE pulled into your project and delete its content.
<liferay-util:buffer>tag, from Liferay’s util taglib to include the original Liferay JSP and assign the JSP’s content to a buffer variable. Assigning the content to a buffer variable enables you to manipulate the original Liferay JSP content as a
For example, you can assign the content of the Liferay JSP to a variable named
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %> <%@ page import="com.liferay.portal.kernel.util.StringUtil" %> <liferay-util:buffer var="html"> <liferay-util:include page="/html/[JSP_file's_path]" /> </liferay-util:buffer>
If you use the buffer assignment from the snippet above, make sure you replace
[JSP_file's_path]with the Liferay JSP’s path. Hint: In Liferay IDE, the JSP file’s path is shown as the Liferay Portal JSP in the Liferay Hook Configuration’s Custom JSPs section in the
Add more content before and/or after the original JSP’s content via the buffer variable. For example, you can import Liferay’s
StringUtilclass and use it to prepend or append content to the buffer variable, like this:
<% html = StringUtil.add( "Stuff I'm adding BEFORE the original content", html, "\n"); html = StringUtil.add( html, "Stuff I'm adding AFTER the original content", "\n"); %>
After adding your desired content to the original content, evaluate the buffer variable as an expression. For example, to evaluate a buffer variable named
<%= html %>
Save your changes.
Deploy the hook plugin.
The original JSP stays in the cache until the cache is cleared or Liferay Portal is restarted. To clear the cache, navigate to Control Panel → Server Administration and click on the appropriate Clear content … buttons in the Resources tab.
Navigate to the custom JSP in Liferay and verify your customization.
The edits you made in the
custom_jsps/[JSP_file's_path] are now visible in your Liferay Portal. In Liferay Portal, The hook you created extends the original JSP’s content with the version of the content you created. You now know how to extend an original Liferay JSP using a hook!