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

  • 相关阅读:
    【转】PHP使用共享内存进程间通信
    【转】php进程间通信--有名管道
    【转】代理模式-php白话示例
    [转]设计的核心任务之三:确保正交性
    pc主板支持独显和集显视频输出
    OpenGL核心技术之抗锯齿
    读书:有钱人想的和你不一样
    js 文本相似度
    js实现获得QQ截图或者微信截图后剪切板的内容clipboardData
    【转】chrome浏览器F12 Network中Timing参数含义
  • 原文地址:https://www.cnblogs.com/flying607/p/3473104.html
Copyright © 2011-2022 走看看