zoukankan      html  css  js  c++  java
  • 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中的现成的拦截器:

  • 相关阅读:
    JavaScript对象的几种创建方式?
    TCP 三次握手,四次挥手
    常用的状态码
    前后端分离的接口规范
    京东架构师:日均 5 亿查询量的ElasticSearch架构如何设计?
    [转] 谈谈Spring中都用到了那些设计模式?
    [转]Post和Get的区别
    [转]17个常用的JVM参数
    从入门到熟悉HTTPS的9个问题
    布式事务和解决方案理论
  • 原文地址:https://www.cnblogs.com/flying607/p/3473104.html
Copyright © 2011-2022 走看看