Site icon Pro Liferay

Liferay JSP Hook Development

Liferay JSP Hook Development


Developing JSP Hook is quite  straight forward and this is one of the most easiest hook to create in Liferay Portal. To create a JSP Hook you just need to know which JSP page you want to customize. You should have liferay source code ready to find out the path of JSP page. Once you located the JSP path you are ready to customize.


Beginners mind storm!!!

Question 1: How would I know which JSP I need customize?

Answer: The main purpose of JSP Hook is to customize a specific JSP page. In your project you will be asked to customize a specific part (i.e., JSP) of the portal. For example you are asked to add extra field in Sign In portlet user registration page. Now its your job to find whats the JSP page responsible for user registration page and then customize it.   

Question 2: I am very new to Liferay Portal. Even I don’t know where the user registration jsp available?

Answer: For the first time it may be little tricky for the beginners.In simple you have to explore it yourself. When you are asked to create any JSP Hook you just try to discover logically whether the JSP that you are going to customize is a part of any portlets or not. Say its part of Sign In Portlet then you look for jsps inside this portlet only.

Question 3: Do I need to customize always JSP inside JSP Hook?

Answer: Yes. But you can customize other resources like js files also inside JSP Hook. For beginners its an advance topic.

Question 4: How many hook do I need to create to customize multiple JSPS?

Answer: It depends. Say you want to customize 10 different jsp pages from different portlets. All the customization can be done in single hook. But this is not recommended. However if you want to customize 10 JSPs of a particular portlet then do all the customization in a single JSP Hook.  

Question 5: I customized a JSP by JSP Hook and its deployed. Now I want to roll back my changes to original.

Answer: Just undeploy the JSP Hook. In simple terms undeploy means go to liferay server and then delete the hook from webapps folder.


Use Case: In this article I am going customize Liferay sign in Portlet to demonstrate Liferay JSP Hook .

Before Customization:

 After Customization:

You can notice that after customization Your IP: label is added to the portlet by the hook. How its done? Follow the steps

Step 1: At first you have to create Liferay plugin project of type Hook. If you don’t know how to create it follow the below post

https://proliferay.com/basics-liferay-hook-development/

Step 2: Lets consider that we have created Liferay Plugin Project of type hook and the name of the project is sign-in-hook. Inside of this project open liferay-hook.xml and add the below contents

<?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>
    <custom-jsp-dir>/custom_jsps</custom-jsp-dir>
</hook>

Note: By this we are just saying that our customization folder is custom_jsps under docroot of the project.

Step 3: In our case we need to customize liferay original jsp file in the path  \liferay-portal-src-6.2.0-ce-ga1\portal-web\docroot\html\portlet\login\login.jsp. To customize it copy this file to sign-in-hook as shown in below

Remember: Under custom_jsps folder we must have to create same directory structure as Liferay source code after docroot directory.

Step 4: Open the login.jsp file and add below line. 

Your IP:<%=com.liferay.portal.util.PortalUtil.getHttpServletRequest(renderRequest).getRemoteAddr()%>

And deploy the hook. Refresh the page to see the changes. That’s all.

 

Download Source Code

Exit mobile version