liferay Hooks in Liferay JSP Hook


JSP hooks are a special liferay plugin that allow to modify core portlet jsp-s, lets say you want to modify the login portlet to show Welcome in my custom login!.

The minimal structure for a Hook Plugin is as follows:

 └── docroot/
     ├── WEB-INF/
     │   ├── src/
     │   ├── lib/
     │   ├── liferay-hook.xml
     │   ├──
     │   └── web.xml
     └── META-INF/
         ├── custom_jsps/
         └── MANIFEST.MF

liferay-hook.xml is the file that distiguishes the type of hook you're using, here you define inside the hook tag the proper parameter for the hook, for JSP hook:

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "">


login.jsp is found in Liferay in /docroot/html/portlet/login/login.jsp, to make a hook of it we need to add a jsp with the same name and path in our custom_jsps folder.

When the hook is deployed, Liferay will look in the liferay-hook.xml for the custom-jsp-dir value and will replace all the portal JSPs with the ones found in this directory. The original jsp's are saved with name <orginal name>.portal.jsp to be restored in case of hook undeployment.

We can even call the original JSPs in the new modified JSP if we want to keep the code making this adaptable to updates or upgrades of the underlying Liferay platform version. To do this, in your custom JSP use the following pattern:

<liferay-util:buffer var="contentHtml">
    <liferay-util:include page="/html/{ JSP file’s path }" />

where { JSP file’s path } in this case will be portlet/login/login.portal.jsp. Doing this is called extending the original jsp.

Then we can add content to it with:

contentHtml = StringUtil.add("Stuff I'm adding BEFORE the original content", contentHtml,"\n");    
contentHtml = StringUtil.add(contentHtml,"Stuff I'm adding AFTER the original content","\n");
<%= contentHtml %>