Today we will discuss about Custom Action Hook in Liferay. In liferay there are some portal events you can see those events in portal.properties.These events are:-
- application.startup.events
- application.shutdown.events
- login.events.pre
- login.events.post
- logout.events.pre
- logout.events.post
- servlet.service.events.pre
- servlet.service.events.post
- servlet.session.create.events
- servlet.session.destroy.events
The action of these events are defined in portal.properties so we need to extend portal.properties to create a custom action.
So lets start step by step:-
Step 1:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.
You can see snapshot in step 2 of Portal Properties Hook in Liferay
Step 2:-Create hook inside project
Right click on project-->New-->Liferay hook Configuration-->tick portal.properties-->Next.
then this pop up is open:-
then click on add another pop up is open where you can select the event click on select event button.This time pop up open with events as:-
Select login.events.pre and click ok.Now Click on New button of previous figure to add our class as:-
Provide Classname, java package and select superclass as Action and click ok.
Now Ok and finish.This will create portal.properties file automatically also provide entry in liferay-hook.xml.
Step 3:-Check Generated Code
By using eclipse ide portal.properties file automatically created as:-
portal.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
#Tue Apr 28 10:59:55 IST 2015 | |
login.events.pre=com.test.MyCustomLoginAction |
and liferay-hook.xml as:-
liferay-hook.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Step 4:-Provide your custom code
Open MyCustomLoginAction.java from package com.test and paste this code:-
MyCustomLoginAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import com.liferay.portal.kernel.events.Action; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.liferay.portal.kernel.events.ActionException; | |
public class MyCustomLoginAction extends Action { | |
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException { | |
System.out.println("My Login Pre event"); | |
} | |
} |
Step 5:-Check Output
Now deploy your hook and login to liferay as soon as you login the eclipse console shows:-
My Login Pre event
Project Structure
You can Download Source code from Custom Action Hook in Liferay
Hope this will Help.....
Related Post:-