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中的现成的拦截器:

 

posted @ 2013-12-13 15:02  剑握在手  阅读(314)  评论(0编辑  收藏  举报
返回顶部↑