liferay Hello User "Name" with hooks


Example

This example will show how to make a simple "Hello User [name]" after the login. The example is based on performing a custom action using a hook

From your command line terminal, navigate to your Plugins SDK’s hooks folder. To create a hook project, you must execute the create script. Here’s the format to follow in executing the script:

create.[sh|bat] [project-name] "[Hook Display Name]"

On Linux and Mac OS X, you’d enter a command similar to the one in this example:

./create.sh Hello-user "Hello User"

On Windows, you’d enter a command similar to the one in this example:

create.bat Hello-user "My Hook"

Liferay IDE’s New Project wizard and the create scripts generate hook projects in your Plugin SDK’s hooks folder. The Plugins SDK automatically appends “-hook” to your project name.

Whether you created your hook project from the Liferay IDE or from the command line, you end up with the same project structure (see before).

  • 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: Session - Startup Events - Shutdown Events - Portal Events
  • 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.
       import com.liferay.portal.kernel.events.Action;
       import javax.servlet.http.HttpServletRequest;
       import javax.servlet.http.HttpServletResponse;
       import com.liferay.portal.model.User;
       import com.liferay.portal.util.PortalUtil;

       public class HelloUser extends Action {
           public void run(HttpServletRequest req, HttpServletResponse res) {
               User user = PortalUtil.getUser(req);
               System.out.println("Hello User "+user.getScreenName());
           }
       }

Important: If your action access the HttpServletRequest object, extend com.liferay.portal.kernel.events.Action; otherwise, extend com.liferay.portal.struts.SimpleAction.

  • 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.

    `login.events.post=HelloUser`
    

    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.

Important: Since portal properties like login.events.pre accept multiple values, you must append your values to the existing values. You can repeatedly modify the properties from additional hooks.

Only modify a portal property that accepts a single value from a single hook plugin. If you modify a property’s value from multiple plugins, Liferay won’t know which value to use.

  • 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>
  • Deploy your hook, go to your hook path and enter ant clean deployyou will see the .war in the dist folder.

Now if you login into liferay, you will see in the server log a message like "Hello user Admin".