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

  • 相关阅读:
    unity UGUI实现类似NGUI切换Sprite的方式
    商业智能系统在税务行业的应用
    MSRDS机器人仿真软件学习资源汇总
    Emotiv脑电设备与RDS机器人仿真初步测试
    unity使用UGUI创建摇杆
    如何利用FineBI做财务分析
    Android4.2.2源码目录结构分析
    一个前端妹子的悲欢编程之路
    推荐一款优雅高效的免费在线APP原型工具
    数据分析概述和理论基础
  • 原文地址:https://www.cnblogs.com/flying607/p/3473104.html
Copyright © 2011-2022 走看看