Hooks can fill a wide variety of the common needs for overriding Liferay core functionality. Whenever possible, hooks should be used in place of Ext plugins, as they ar hot- deployable and more forward compatible. Some common scenarios which require the use of a hook are the need to perform custom actions on portal startup or user login, overwrite or extend portal JSPs, modify portal properties, or replace a portal service with your own implementation.
Creating a Hook
1. First go to the hook directory in liferay sdk…..(before see how to configure buld.xml for ant in previous one how to create portlet and theme)
2. Then using this command you can create a hook.
(./create.sh example "Example" -----------------à on Linux
create.bat example "Example" -------------------à on Windows)
3. You should get a BUILD SUCCESFUL message from Ant, and there will now be a new folder inside of the hooks folder in your Plugins SDK. Notice that the Plugins SDK automatically appensds “-hook” to the project name when creating this folder.
4. Deploying hooks first go to hooks/example-hook directory using Terminal or command prompt. Then enter this command for deploying to liferay server.
(ant deploy) (in build($username).properties should be configured)
5. You should get a BUILD SUCCESSFUL message, which means that your hook is now being deployed. If you switch to the terminal window running Liferay, and wait for a few seconds, you should see the message “Hook for example-hook is available for use.”
Then Show how to performing custom action
1. First, create the directory example-hook/docroot/WEB-INF/src/com/sample/hook, and create the file LoginAction.java inside it with the following content.
package com.sample.hook;
import com.liferay.portal.kernel.events.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAction extends Action {
public void run(HttpServletRequest req, HttpServletResponse res) {
System.out.println("## My custom login action");
}
}
2. Next, create the file portal.properties inside example-hook/docroot/WEB-INF/src with following content.
login.events.pre=com.sample.hook.LoginAction
3. Finally, edit liferay-hook.xml inside example-hook/docroot/WEB-INF
4. Enter this line between <hook></hook> tabs.
<portal-properties>portal.properties</portal-properties>
5. Deploy your hook again and wait for it to complete. Then log out and back in, and you should see our custom message in the terminal window running Liferay.
Dileepa Sisila Chandrasekera
Computer Science & Engineering
University of Moratuwa
Trainee at hsenid mobile solution (pvt) Ltd.
No comments:
Post a Comment