struts struts拦截器(过滤器)

在struts中尽量避免自定义拦截器,因为大部分需要自己定义拦截器的时候,设计思路就不对了。大部分拦截器框架都有给你定义好了。而且如果在struts中定义拦截器相当于和这个框架绑定了,假如以后要扩展或者换框架,就可能要重新在新框架中写个拦截器。

总之尽量不要自定义struts的拦截器。再次引用一句谚语:Don't Reinvent the Wheel。

 

拦截器的使用实践的是面向切面编程思想

 

拦截器的使用格式:

 

 

<?xml version="1.0" encoding="UTF-8" ?>

 

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"

    "http://struts.apache.org/dtds/struts-2.1.dtd">

 

<struts>

       <constant name="struts.devMode" value="true"></constant>

       <package name="test" namespace="/" extends="struts-default">

              <interceptors>

                     <interceptor name="my" class="test.interceptor.MyInterceptor"></interceptor>

              </interceptors>

 

              <action name="test" class="com.bjsxt.action.TestAction">

                     <result>/test.jsp</result>

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

                     <interceptor-ref name="defaultStack"></interceptor-ref><!-- 默认的拦截器不要被覆盖,此处书写顺序表示默认的拦截器在外边,自定义的在里边 -->

              </action>

       </package>

</struts>

 

自定义拦截器写法:

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

 

public class MyInterceptor implements Interceptor {

       public void destroy() {

       }

 

       public void init() {

       }

 

       public String intercept(ActionInvocation invocation) throws Exception {

              long start = System.currentTimeMillis();

              String r = invocation.invoke();

              long end = System.currentTimeMillis();

              System.out.println("action time = " + (end - start));

              return r;

       }

}

 

 

struts中的现成的拦截器:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值