• java strtus2 拦截器(Interceptors)


     在strtus2 中有一个比较重要的东西就是拦截器(Interceptors)

    拦截器可以做到在已有的业务中插入一块共通的,比如在一个业务中,直接插入一串登录功能,就不用去每个页面一个个去显示是否登录,

    直接在拦截器管理,就等于在总入口直接管理了,也不会出现,个别直接通过连接能进入网站的。

    或者说,在没进入一个功能的时候记录一下客户的操作,这样与其在一个个的action分别记录,直接在拦截器去记录会更加的方便

    或者是记录log

    这里来简单讲解下拦截器的使用。

    拦截器是在struts.xml中进行配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
        "http://struts.apache.org/dtds/struts-2.1.7.dtd">
    <struts>
     <!-- 请求参数的编码方式--> 
        <constant name="struts.i18n.encoding" value="UTF-8"/> 
       <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开--> 
        <constant name="struts.action.extension" value="action"/>
         <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  --> 
        <constant name="struts.configuration.xml.reload" value="true"/> 
        <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  --> 
        <constant name="struts.devMode" value="false"/>   
        <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  --> 
        <constant name="struts.serve.static.browserCache" value="false" /> 
        <!-- 配置返回结果去哪里寻找 -->
        <constant name="struts.convention.result.path" value="/" />
         <!-- 配置类去哪里寻找 -->
        <constant name="struts.convention.package.locators" value="action" />  
        <!-- 动态方法调用打开,可以通过感叹号调用action里面的方法 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true" />
         
         
    <!--     <package name="Menu" namespace="/Menu" extends="struts-default">
           <action name="mainpage" class="com.almostman.action.MainPageAction">
             <result name="success">/success.jsp</result>
             <result name="input">/input.jsp</result>
           </action>
        </package>  -->
        <!-- 这里的{1}表示*的值 -->
        <package name="Menu" namespace="/Menu" extends="struts-default">
        
              <interceptors>
                <!-- 定义拦截器 
                    name:拦截器名称
                    class:拦截器类路径
                 -->
                <interceptor name="timer" class="com.almostman.action.TimerInterceptor"></interceptor>
                <interceptor name="logger" class="com.almostman.action.LoggerInterceptor"></interceptor>
                <!-- 定义拦截器栈 -->
                <interceptor-stack name="mystack">
                    <!-- 在有自定义拦截器的时候一定要将默认拦截器放在最上面,不然会出现参数无法传递 -->
                    <interceptor-ref name="defaultStack" />
                    <interceptor-ref name="timer"/>
                    <interceptor-ref name="logger"/>
                </interceptor-stack>
            </interceptors>
            
            <!-- 定义默认的拦截器 每个Action都会自动引用
                     如果Action中引用了其它的拦截器 默认的拦截器将无效 -->
            <default-interceptor-ref name="mystack"></default-interceptor-ref>
                    <!-- 全局results配置 -->
            
            
           <action name="*" class="com.almostman.action.{1}Action" >
             <result name="success">/{1}.jsp</result>
             <result name="input">/input.jsp</result>
           </action>
        </package> 
        
        <package name="Test" namespace="/Test" extends="struts-default">
           <action name="HelloPage" class="com.almostman.action.HelloPageAction" >
             <result name="success"  type="velocity" >/HelloPage.vm</result>
             <result name="input">/error.jsp</result>
           </action>
        </package> 
    
    
    </struts>

    这里自定义了两个拦截器的类,一个用来记录 用户操作的时间,一个用来记录log。

    package com.almostman.action;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    public class TimerInterceptor extends AbstractInterceptor
    {
    
        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception
        {
            // TODO Auto-generated method stub
            System.out.println("在正确的时间登录");
            
            return actionInvocation.invoke();
        }
    
    
    }
    package com.almostman.action;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    public class LoggerInterceptor extends AbstractInterceptor
    {
    
        public LoggerInterceptor()
        {
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception
        {
            // TODO Auto-generated method stub
            System.out.println("在这里输出log");
            return actionInvocation.invoke();
        }
    
    }

    执行后一定要invoke出去,这样才能走到action,然后就是按照action的正常路程去返回给jsp页面。

    ,以上运行结果是:

    能发下,拦截器生效,并且正常的走到了jsp的页面。

    源码地址:http://pan.baidu.com/s/1cpdJdG

  • 相关阅读:
    CRUD工程师——嵌入式Web容器
    CRUD工程师——SpringBoot启动原理
    CRUD工程师——日志
    CRUD工程师——慢SQL
    CRUD工程师——索引
    前端专业术语: shim 和 Polyfill,了解下
    H5之postMessage 。实现跨域
    摘抄详细的VUE生命周期
    如何在不使用三大地图的KEY和相关组件的情况下,直接传参数到相关的H5地图
    Mac下通过brew安装指定版本的nodejs
  • 原文地址:https://www.cnblogs.com/sunxun/p/6634084.html
Copyright © 2020-2023  润新知