Performing a Custom Action Using a Hook

Hooks are useful for triggering custom actions on common portal events, like user login or system startup. The actions for each of these events are defined in portal.properties, so you need to extend this file to create a custom action. Hooks make this a simple task. It’s time to explore how to perform a custom action using a hook!

In this tutorial, you’ll create a custom hook that performs a custom action for user login. You could similarly create a custom action for other portal events defined in portal.properties. It’s time to get started!

  1. Determine the event on which you want to trigger your custom action. Look in the portal.properties documentation to find the matching event property. Hint: the event properties have .event in their name. There are session, startup, shutdown, and portal event properties in the following sections of the portal.properties documentation:

    Note the property name of the event related to your action.

  2. In your hook project, create a Java class that extends the com.liferay.portal.kernel.events.Action class. Override the Action.run(HttpServletRequest, HttpServletResponse) method.

    For example, here’s a class named LoginAction that extends Action and overrides its run method:

    package com.liferay.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");
        }
    }
    
  3. Create a properties file, portal.properties, inside your hook project’s docroot/WEB-INF/src folder. Then add the name of the portal event property that corresponds to the event on which you want to perform your action. Specify your action class’ fully qualified name as the property’s value.

    For example, to perform a class’ action just prior to the portal logging in a user, you’d specify the login.events.pre property with your action class as its value. It could look like this property setting.

    login.events.pre=com.liferay.sample.hook.LoginAction
    
  4. Edit your docroot/WEB-INF/liferay-hook.xml file and add your hook’s portal properties file name as the value for the <portal-properties>...</portal-properties> element within your hook’s <hook>...</hook> element.

    For example, if your hook’s properties file name is portal.properties, you’d specify this element:

    <portal-properties>portal.properties</portal-properties>
    
  5. Deploy your hook.

  6. Perform steps to trigger your action and verify that the action was executed.

Great! You’ve created a hook that triggers a custom action on a common portal event. You now know the basic steps required to perform a custom action using a hook in Liferay Portal.

Related Topics

MVC Portlets

Overriding and Adding Struts Actions

« Overriding a Portal Service Using a HookCreating Model Listeners »
Was this article helpful?
0 out of 1 found this helpful