我使用liferay 5.2和tomcat 6
我想在用户连接到我的应用程序
时保存idportal和date登录在liferay中被解聚
我应该找到负责登录的事件
我使用的代码会在此事件发生后进行更新: onAfterCreate
import com.liferay.portal.model.User;
.........
........
public void onAfterCreate(User user)
throws ModelListenerException
{
System.out.println("-------------- User Creation::" + user.getScreenName());
try
{
Employee employee = getEmployee(user.getScreenName());
if (employee != null)
updateEmployee(employee, user, false);
else
updateEmployee(new Employee(), user, true);
}
catch (Exception e) {
e.printStackTrace();
}
}
所以我应该像前面的代码一样使用相同的代码
idportal是: user.getScreenName()
日期登录我认为 user.getLastLoginDate()
但是我没有找到登录后会被拒绝的事件
已更新:
我按照你的步骤
在liferay-hook.xml中的我添加了这段代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.1.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_1_0.dtd">
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
并在portal.properties中添加
login.events.post=com.liferay.hooks.user.LoginPostAction
我创建了java类:LoginPostAction.java
package com.liferay.hooks.user;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
public class LoginPostAction
{
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {
System.out.println(" the user is connected ");
}
}
在本例中导入导入com.liferay.portal.kernel.events.Action;未使用
如您所见,我希望此代码在登录后显示消息
但是当我部署这个例子时,我有这个错误:
Caused by: java.lang.ClassCastException: com.liferay.hooks.user.LoginPostAction cannot be cast to com.liferay.portal.kernel.events.Action
at com.liferay.portal.deploy.hot.HookHotDeployListener.initEvent(HookHotDeployListener.java:591)
at com.liferay.portal.deploy.hot.HookHotDeployListener.initEvents(HookHotDeployListener.java:643)
at com.liferay.portal.deploy.hot.HookHotDeployListener.doInvokeDeploy(HookHotDeployListener.java:237)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployListener.java:98)
... 31 more
已更新:
我想显示连接用户的ScreenName和登录日期,
我已经使用此代码显示ScreenName user.getScreenName()
,对于登录日期,我认为我应该使用user.getLastLoginDate(
)并使用此导入
import com.liferay.portal.model.User;
但我没有找到在运行功能
中使用此代码的方法package com.liferay.hooks.user;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
public class LoginPostAction extends Action
{
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {
System.out.println(" the user is connected ");
// here I want to display the ScreenName and login date of user connected
}
}
我尝试使用此代码:
package com.liferay.hooks.user;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
public class LoginPostAction extends Action
{
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {
System.out.println(" the user is connected ");
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
User user = themeDisplay.getUser();
System.out.println("user "+user.getScreenName());
}
}
我在portal.properties中创建了这段代码:
login.events.post=com.liferay.hooks.user.LoginPostAction
servlet.service.events.post=com.liferay.hooks.user.LoginPostAction
但我有这个错误:
Caused by: java.lang.NullPointerException
at com.liferay.hooks.user.LoginPostAction.run(LoginPostAction.java:25)
at com.liferay.portal.kernel.events.InvokerAction.run(InvokerAction.java:52)
at com.liferay.portal.events.EventsProcessorImpl.processEvent(EventsProcessorImpl.java:92)
答案 0 :(得分:2)
您正在使用的聆听者是&#34;模型聆听者&#34;并且它们用于添加\ update \ delete事件。
收听登录事件。您可以执行以下操作:
在liferay-hook.xml
文件中,将portal-properties
添加到hook
元素。例如,
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
然后创建/docroot/WEB-INF/src/portal.properties
。
添加以下行:
login.events.post=com.test.events.LoginPostAction
然后扩展com.liferay.portal.kernel.events.Action
。
实施:
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException;