编写一个计时器拦截器

第一步:创建类MyTimeInterceptor

public class MyTimeInterceptor extends AbstractInterceptor{

@Override

public String intercept(ActionInvocation arg0) throws Exception {

//预处理工作

System.out.println("thi is i1 start");

long startTime=System.currentTimeMillis();

//执行后续拦截器或Action

String result=arg0.invoke();

        //后续处理工作

long execTime=System.currentTimeMillis()-startTime;

System.out.println("The interval time is "+execTime+" ms");

return result;

}

}

第二步:在struts.xml文件中定义拦截器

<interceptors>

<interceptor name="myTimer" class="base.MyTimeInterceptor"></interceptor>

</interceptors>

:拦截器的定义需要放在package标签的最上边

第三步:在需要添加拦截器的action中配置

<action name="reg" class="base.RegAction">

<result name="success">/regsuccess.jsp</result>

<result name="fail">/reg.jsp</result>

<result name="input">/reg.jsp</result>

<interceptor-ref name="myTimer"></interceptor-ref>

</action>

注意:如果使用上述拦截器,那么reg中之前struts提供的数据校验,数据自动封装等功能将无法使用,不过我们可以加再次加入默认的拦截器

struts-default.xml中定义一个defaultStack拦截器栈,并将其指定为默认拦截器

只要在定义包的过程中继承struts-default包,那么defaultStack将是默认的拦截器 

<action name="reg" class="base.RegAction">

<result name="success">/regsuccess.jsp</result>

<result name="fail">/reg.jsp</result>

<result name="input">/reg.jsp</result>

<interceptor-ref name="myTimer"></interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

</action>

修改为上边代码后strtus默认提供的功能就又可以使用了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值