码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2的处理结果(四)——PreResultListener监听器

时间:2016-11-12 02:02:26      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:import   get   监听   系统   tor   font   family   bsp   参数   

Struts2的处理结果(四)

      ——PreResultListener监听器

1.PreResultListener是一个监听器接口,他在Action完成控制处理之后,系统转入实际物理视图资源之间被回调:

  Action——PreResultListener——物理视图资源

2.PreResultListener监听器可以由Action或者拦截器添加,添加PreResultListener的方法如下:

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class Test2Action implements Action{
    //封装两个请求参数
    private String username;
    private String password;
    //setter、getter方法
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return this.username;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getPassword(){
        return this.password;
    }
    @Override
    public String execute() throws Exception {
        //获取ActionInvocation对象
        ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
        //使用获取到的ActionInvocation对象的addPreResultListener()方法添加PreResultListener监听器
        //PreResultListener接口中有个beforeResult()方法,该方法将在系统转入物理视图资源前被调用
        invocation.addPreResultListener(new PreResultListener() {
            @Override
            //beforeResult()方法中传递的两个参数:
            //ActionInvocation:ActionInvocation实例
            //String:转入此方法的逻辑视图名称
            public void beforeResult(ActionInvocation arg0, String arg1) {
                // TODO Auto-generated method stub
                //可以在此方法中在返回Result前加入一个额外的参数
                //也可以在此方法中加入日志等
            }
        });
        return null;
    }

}

3.PreResultListener的作用,加入额外参数,实现日志。

 

Struts2的处理结果(四)——PreResultListener监听器

标签:import   get   监听   系统   tor   font   family   bsp   参数   

原文地址:http://www.cnblogs.com/ljiwej/p/6056041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!