PreResultListener使用

时间:2023-03-09 01:09:33
PreResultListener使用
PreResultListener是一个监听器接口,可以在Action处理完之后,系统转入实际视图前被回调。
Struts2应用可以给Action、拦截器添加PreResultListener监听器,添加PreResultListener可以通过ActionInvocation的addPreResultListener()方法完成:
 // Action默认包含的控制逻辑
public String execute() throws Exception
{
ActionInvocation invocation = ActionContext
.getContext().getActionInvocation();
invocation.addPreResultListener(new PreResultListener()
{
public void beforeResult(ActionInvocation invocation,
String resultCode)
{
System.out.println("返回的逻辑视图名字为:"
+ resultCode);
// 在返回Result之前加入一个额外的数据。
invocation.getInvocationContext().put("extra"
, new java.util.Date() + "由"
+ resultCode + "逻辑视图名转入");
// 也可加入日志等
}
});
if (getUsername().equals("crazyit.org")
&& getPassword().equals("leegang") )
{
ActionContext.getContext().getSession()
.put("user" , getUsername());
addActionMessage("欢迎," + getUsername() + ",您已经登录成功!");
return SUCCESS;
}
return ERROR;
}