資料 リソース
Liferayは、コミュニティにてテクノロジーをより良く使うために役立つ豊富なリソースと知識を提供しています。
Performing a Custom Action
Another common use of hooks is to perform custom actions on certain common portal events, such as user log in or system startup. The actions that are performed on each of these events are defined in portal.properties, which means that in order to create a custom action we will also need to extend this file. Fortunately, this is extremely easy using a hook.
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");
}
}
Next, create the file portal.properties inside example-hook/docroot/WEB-INF/src with the following content:
login.events.pre=com.sample.hook.LoginAction
Finally, edit liferay-hook.xml inside example-hook/docroot/WEB-INF and add the following line above <custom-jsp-dir>:
<portal-properties>portal.properties</portal-properties>
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.
There are several other events that you can define custom actions for using hooks. Some of these actions must extend from com.liferay.portal.kernel.events.Action, while others must extend com.liferay.portal.struts.SimpleAction. For more information on these events, see the portal.properties configuration file for your version of Liferay in: http://www.liferay.com/community/wiki/-/wiki/Main/Portal+Properties
Extending and Overriding portal.properties
In our hook, we modified the login.events.pre portal property. Since this property accepts a list of values, our value was appended to the existing values. It is safe to modify these portal properties from multiple hooks, and they will not interfere with one another. Some portal properties only accept a single value, such as the terms.of.use.required property, which can be either true or false. You should only modify these properties from one hook, otherwise Liferay will not know which value to use. You can determine which type a particular property is by looking in portal.properties.
Not all portal properties can be overridden in a hook. A complete list of the available properties can be found in the DTD for liferay-hook.xml in the definitions folder of the Liferay source code. In addition to defining custom actions, hooks can also override portal properties to define model listeners, validators, generators, and content sanitizers.