您的位置:首页 > 其它

PreResultListener使用

2016-08-02 16:49 435 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: