Hooks are commonly used to override web resources, found in portal-web
in the
Liferay source. You can use a hook to override JSP files, JSPF files, JavaScript
files, or images.
Important: A JSPF change takes effect only when you modify a JSP that includes the JSPF.
Replacing a portal JSP is a simple task with hooks. Let’s create and deploy a hook to modify your portal’s Terms of Use page.
-
Use the hook project we created earlier or create a new hook project.
-
Open the
liferay-hook.xml
file from your project’sdocroot/WEB-INF
folder in Liferay IDE. By default, it opens in Overview mode. This mode, gives you a graphical user interface for developing your hook. You can toggle between overview mode and source mode via their respective tabs for theliferay-hook.xml
file’s editor. -
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_jsps
, by clicking the icon that has the three yellow diamonds. -
Add to the listing of custom JSPs by clicking the plus icon and specifying Portal’s
html/portal/terms_of_use.jsp
file. Hint, the browse icon on the right-hand side within the custom JSP text field simplifies finding the JSP you want to customize. It lets you scroll through the JSPs that are accessible and lets you specify key words to narrow your search. -
Open your hook’s
docroot/META-INF/custom_jsps/html/portal/terms_of_use.jsp
file and modify it as necessary. -
Deploy your hook and wait until it is deployed successfully.
-
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.
-
Create a new user and log in. The Terms of Use page should include the changes you made above.
Now there are two Terms of Use JSP files in the
liferay-portal-[version]/tomcat-[tomcat-version]/webapps/ROOT/html/portal
directory. One is called terms_of_use.jsp
and another
terms_of_use.portal.jsp
. terms_of_use.jsp
is your hook’s version, while
terms_of_use.portal.jsp
is the original. To revert back to the original,
undeploy your hook. Your replacement JSP is removed, and the .portal.jsp
file
is automatically renamed, taking its place. You can override any JSP in the
Liferay core, while retaining thew ability to easily revert your changes.
However, it’s not possible to override the same JSP from multiple hooks; Liferay
won’t know which version to use.
Next, we’ll look at a different way to customize a JSP.