Struts2拦截器的结果监听器

结果监听器是指在execute方法执行完,系统会调用结果监听器。结果监听器是一个类,该类实现了PreResultListener接口。PreResultListener接口有一个beforeResult方法,该方法有两个参数,invocation和resultCode,其中invocation是intercept方法中的invocation,resultCode表示execute方法返回的字符串。

以下是演示代码

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

public class MyPreResultListener implements PreResultListener{
    public void beforeResult(ActionInvocation invocation, String resultCode) {
        System.out.println("execute的返回值:" + resultCode);
    }
}

要使用结果监听器,还需要在拦截器中用ActionInvocation的addPreResultListener方法进行注册,演示代码如下

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class ExecuteTimeInterceptor extends MethodFilterInterceptor {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String doIntercept(ActionInvocation actionInvocation) throws Exception{
        //注册结果监听器
        actionInvocation.addPreResultListener(new MyPreResultListener());
        System.out.println(getName() + " 开始执行");
        long start = System.currentTimeMillis();
        String result = actionInvocation.invoke();
        long end = System.currentTimeMillis();
        System.out.println(getName() + " 执行Action方法的时间:" + (end-start) + "毫秒");
        System.out.println(getName() + " 执行结束");
        return result;
    }
}

以上自建的监听器运行结果是这样的:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值