Custom Action by Liferay Hook

Custom Action By Hook

aimWe can develop hook to ย implement our own Custom Action. So what does it mean by Custom Action? Just open portal.properties in your liferay source code. You will see many entries which contains events. For exampleย login.events.pre,ย login.events.post and many more. These are simply called actions and every action has its specific task. Theย login.events.preย events (or action) does something ย before user log in and similarlyย login.events.post does some specific task after login. If you look into portal.properties you will see many other events or actions. So if we want our own action (or event) then how are we going to implement it. ย By Hook we can implement our Custom Action. This article explains the details about it.


Create Custom Action By Hook

In this article we will demonstrate how to create our own Pre Login Action by Hook

ย 

1. Create Liferay Plugin project of type Hook

The first step is you have to create Liferay Plugin Project of type hook. Follow the postย 

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

2.ย Create newย classย CustomPreLoginAction

This is the class we will call as Custom Action. The content of the class should be like thisย 

package com.proliferay.demo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;

/**
 *
 * @author Hamidul Islam
 *
 */

public class CustomPreLoginAction extends Action{

	@Override
	public void run(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws ActionException {
		/**
		 * Write your custom code here
		 */
		System.out.println("Invoking this line before Login");

	}

}

Note 1: If your Custom Action needs http servlet request and response extends the class com.liferay.portal.kernel.events.Action

Note 2: If your Custom Action does not require any request response object then extends the classย com.liferay.portal.struts.SimpleAction

Note 3: Bothย com.liferay.portal.kernel.events.Action andย com.liferay.portal.struts.SimpleAction are abstract class. Weย must add unimplemented method in our Custom Action.

Note 4:ย  In run method write your custom code.ย 

3.ย Tell Liferay thatย CustomPreLoginAction is the pre login action

This is simply done by adding below in ourย portal.properties

#Tue Dec 09 12:39:53 IST 2014
login.events.pre=com.proliferay.demo.CustomPreLoginAction

4.ย Yo have to tell Liferay where the portal.properties exist
This is done by

<?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>
<portal-properties>portal.properties</portal-properties>
</hook>

Look the source code for details.

This is all about creating Custom Action by Hook. Deploy the hook. Before log in you will see the message

//ย System.out.println(“Invoking this line before Login“);

As mentioned in our custom action.ย 

 

Download Source Code

 

ย 

About The Author

3 thoughts on “Custom Action by Liferay Hook”

  1. How to import the zip file into eclise?

    That zip file is created by you? or liferay’s sign-in portlet?

    so many doubts are there.. Please explain me

    Regards,

    Rithika

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top