<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Struts2 攔截器

    Posted on 2010-03-29 23:17 Gavin.lee 閱讀(494) 評論(0)  編輯  收藏 所屬分類: SSH2 --Struts2

    一、攔截器定義
    攔截器,在AOP(Aspect-Oriented Programming)中用于在某個方法或字段被訪問之前,進行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實現策略。

    在Webwork的中文文檔的解釋為——攔截器是動態攔截Action調用的對象。它提供了一種機制可以使開發者可以定義在一個action執行的前后執行的代碼,也可以在一個action執行前阻止其執行。同時也是提供了一種可以提取action中可重用的部分的方式。

    談到攔截器,還有一個詞大家應該知道——攔截器鏈(Interceptor Chain,在Struts 2中稱為攔截器棧Interceptor Stack)。攔截器鏈就是將攔截器按一定的順序聯結成一條鏈。在訪問被攔截的方法或字段時,攔截器鏈中的攔截器就會按其之前定義的順序被調用。


    二、攔截器的流程  訪問: http://localhost:8080/interceptor1/test_interceptor.action
    1.攔截器的觸發頁面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()    + path + "/";
    %>
    <%@ taglib prefix="s" uri="/struts-tags"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        
    <head>
        
    <base href="<%=basePath%>">
        
    <title>My JSP 'index.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
        
    <meta http-equiv="cache-control" content="no-cache">
        
    <meta http-equiv="expires" content="0">
        
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        
    <meta http-equiv="description" content="This is my page">
        
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        
    -->
        
    </head>

        
    <body>
            
    <s:form action="test_interceptor">
                
    <s:textfield name="username" label="username"></s:textfield>
                
    <s:submit name="submit"></s:submit>
            
    </s:form>
        
    </body>
    </html>

    2.Struts2配置文件,攔截器的映射

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
    >


    <struts>
        
    <package name="interceptor1" extends="struts-default">

            
    <!-- 定義攔截器 -->
            
    <interceptors>
                
    <interceptor name="myInterceptor" class="cn.edu.hdu.interceptor.MyInterceptor"></interceptor>
            
    </interceptors>

            
    <!-- 配置action -->
            
    <action name="test_interceptor" class="cn.edu.hdu.action.Test_InterceptorAction">
                
    <result name="success">/success.jsp</result>
                
    <result name="input">/test.jsp</result>
                
    <!-- 將聲明好的攔截器插入action中 -->
                
    <interceptor-ref name="myInterceptor" />
                
    <interceptor-ref name="defaultStack" />
            
    </action>
        
    </package>
    </struts>

     3.攔截器MyInterceptor

    package cn.edu.hdu.interceptor;

    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;

    /**
     * 自定義攔截器
     * 
    @author gavin
     * @date 2010-3-29
     
    */

    public class MyInterceptor implements Interceptor {
        
    /**
         * 
         
    */

        
    private static final long serialVersionUID = -4855835386612768133L;

        
    public void destroy() {
            System.out.println(
    "destroy");
        }


        
    public void init() {
            System.out.println(
    "攔截器已經被加載");
        }


        
    public String intercept(ActionInvocation invocation) throws Exception {
            System.out.println(
    "調用intercept方法");
            
    // invocation.invoke()方法檢查是否還有攔截器 有的話繼續調用余下的攔截器 沒有了 執行action的業務邏輯
            String result = invocation.invoke();
            
    return result;
        }

    }

    4.通過攔截器后進入Action

    package cn.edu.hdu.action;

    import com.opensymphony.xwork2.ActionSupport;

    public class Test_InterceptorAction extends ActionSupport {
        
    /**
         * 
         
    */

        
    private static final long serialVersionUID = 5138320466369666402L;
        
    private String username;

        
    public String getUsername() {
            
    return username;
        }


        
    public void setUsername(String username) {
            
    this.username = username;
        }


        
    public String execute() throws Exception {
            
    return SUCCESS;
        }


        
    public String test() throws Exception {
            System.out.println(
    "此時所有攔截器完畢,已經調用了action中的test方法");
            
    return SUCCESS;
        }


    }

    5.通過Action處理后的視圖頁面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      
    <head>
        
    <base href="<%=basePath%>">
        
    <title>My JSP 'success.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
        
    <meta http-equiv="cache-control" content="no-cache">
        
    <meta http-equiv="expires" content="0">    
        
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        
    <meta http-equiv="description" content="This is my page">
        
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        
    -->
        
    </head>
      
        
    <body>
            去看看控制臺吧
        
    </body>
    </html>


    三、Struts2默認的攔截器

    <!--struts2中攔截器的定義-->   
            
    <interceptors>   
            
    <!--實現在不同請求中相似參數別名的準換-->   
                
    <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>   
                
    <!--與Spring整合時自動裝配的攔截器-->   
                
    <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>   
                
    <!--構建一個action鏈,使當前action可以訪問前一個action,與<result-type="chain" />配合使用-->   
                
    <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>   
                
    <!--負責類型轉換的攔截器-->   
                
    <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>   
                
    <!--使用配置的name,value來是指cookies -->   
                
    <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>   
               
    <!--負責創建httpSession-->   
                
    <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />   
                
    <!--輸出調試信息-->   
                
    <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />   
                
    <!--擴展引用-->   
                
    <interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>   
                
    <!--后臺執行action負責發送等待畫面給用戶-->   
                
    <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>   
                
    <!--異常處理-->   
                
    <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>   
                
    <!--文件上傳,解析表單域的內容-->   
                
    <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>   
                
    <!--支持國際化-->   
                
    <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>   
               
    <!--日志記錄-->   
                
    <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>   
                
    <!--模型攔截器,當action實現了ModelDriven接口時,負責把getModel的結果放入valueStack中-->   
                
    <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>   
                
    <!--有生命周期的ModelDriven-->   
                
    <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>   
                
    <!--負責解析請求中的參數,并賦值給action中對應的屬性-->   
                
    <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>   
                
    <!--實現該Preparable接口的action,會調用攔截器的prepare方法-->   
                
    <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>   
                
    <!--負責將action 標簽下的param參數值傳遞給action實例-->   
                
    <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>   
                
    <!--范圍轉換-->   
                
    <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>   
                
    <!--用于訪問Servlet API-->   
                
    <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>   
                   
                
    <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>   
                
    <!--輸出action執行時間-->   
                
    <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>   
                
    <!--防止表單重復提交-->   
                
    <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>   
                
    <!--與token攔截器相似,只是把token保存到HttpSession-->   
                
    <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>   
                
    <!--負責表單字段的驗證 *-validation.xml-->   
                
    <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>   
                
    <!--負責執行action的validate()-->   
                
    <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>   
                
    <!--存儲和重新獲取Action 消息/錯誤/字段錯誤為Action,實現ValidationAware接口到seesion-->   
                
    <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />   
                
    <!--添加自動checkbox處理代碼,這樣檢探測checkbox和添加它作為一個參數使用默認值(通常’false’).使用一個指定名字隱藏字段探測沒提交的checkbox-->   
                
    <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />   
                
    <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />   
                
    <!--JAAS服務攔截器-->   
                
    <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />   
      
                
    <!-- 一個基本的攔截器棧 -->   
                
    <interceptor-stack name="basicStack">   
                    
    <interceptor-ref name="exception"/>   
                    
    <interceptor-ref name="servletConfig"/>   
                    
    <interceptor-ref name="prepare"/>   
                    
    <interceptor-ref name="checkbox"/>   
                    
    <interceptor-ref name="params"/>   
                    
    <interceptor-ref name="conversionError"/>   
                
    </interceptor-stack>   
      
                
    <!-- 簡單的validtion和webflow棧 -->   
                
    <interceptor-stack name="validationWorkflowStack">   
                    
    <interceptor-ref name="basicStack"/>   
                    
    <interceptor-ref name="validation"/>   
                    
    <interceptor-ref name="workflow"/>   
                
    </interceptor-stack>   
      
                
    <!-- 文件上傳的攔截器棧 -->   
                
    <interceptor-stack name="fileUploadStack">   
                    
    <interceptor-ref name="fileUpload"/>   
                    
    <interceptor-ref name="basicStack"/>   
                
    </interceptor-stack>   
      
                
    <!-- model-driven 棧  -->   
                
    <interceptor-stack name="modelDrivenStack">   
                    
    <interceptor-ref name="modelDriven"/>   
                    
    <interceptor-ref name="basicStack"/>   
                
    </interceptor-stack>   
      
                
    <!-- action鏈的攔截器棧 -->   
                
    <interceptor-stack name="chainStack">   
                    
    <interceptor-ref name="chain"/>   
                    
    <interceptor-ref name="basicStack"/>   
                
    </interceptor-stack>   
      
                
    <!--  i18n 攔截器棧 -->   
                
    <interceptor-stack name="i18nStack">   
                    
    <interceptor-ref name="i18n"/>   
                    
    <interceptor-ref name="basicStack"/>   
                
    </interceptor-stack>   
      
                
    <!-- 結合preparable和ModenDriven攔截器-->   
                
    <interceptor-stack name="paramsPrepareParamsStack">   
                    
    <interceptor-ref name="exception"/>   
                    
    <interceptor-ref name="alias"/>   
                    
    <interceptor-ref name="params"/>   
                    
    <interceptor-ref name="servletConfig"/>   
                    
    <interceptor-ref name="prepare"/>   
                    
    <interceptor-ref name="i18n"/>   
                    
    <interceptor-ref name="chain"/>   
                    
    <interceptor-ref name="modelDriven"/>   
                    
    <interceptor-ref name="fileUpload"/>   
                    
    <interceptor-ref name="checkbox"/>   
                    
    <interceptor-ref name="staticParams"/>   
                    
    <interceptor-ref name="params"/>   
                    
    <interceptor-ref name="conversionError"/>   
                    
    <interceptor-ref name="validation">   
                        
    <param name="excludeMethods">input,back,cancel</param>   
                    
    </interceptor-ref>   
                    
    <interceptor-ref name="workflow">   
                        
    <param name="excludeMethods">input,back,cancel</param>   
                    
    </interceptor-ref>   
                
    </interceptor-stack>   
      
                
    <!--定義默認的攔截器棧  -->   
                
    <interceptor-stack name="defaultStack">   
                    
    <interceptor-ref name="exception"/>   
                    
    <interceptor-ref name="alias"/>   
                    
    <interceptor-ref name="servletConfig"/>   
                    
    <interceptor-ref name="prepare"/>   
                    
    <interceptor-ref name="i18n"/>   
                    
    <interceptor-ref name="chain"/>   
                    
    <interceptor-ref name="debugging"/>   
                    
    <interceptor-ref name="profiling"/>   
                    
    <interceptor-ref name="scopedModelDriven"/>   
                    
    <interceptor-ref name="modelDriven"/>   
                    
    <interceptor-ref name="fileUpload"/>   
                    
    <interceptor-ref name="checkbox"/>   
                    
    <interceptor-ref name="staticParams"/>   
                    
    <interceptor-ref name="params">   
                      
    <param name="excludeParams">dojo\..*</param>   
                    
    </interceptor-ref>   
                    
    <interceptor-ref name="conversionError"/>   
                    
    <interceptor-ref name="validation">   
                        
    <param name="excludeMethods">input,back,cancel,browse</param>   
                    
    </interceptor-ref>   
                    
    <interceptor-ref name="workflow">   
                        
    <param name="excludeMethods">input,back,cancel,browse</param>   
                    
    </interceptor-ref>   
                
    </interceptor-stack>   
      
                
    <interceptor-stack name="completeStack">   
                    
    <interceptor-ref name="defaultStack"/>   
                
    </interceptor-stack>   
      
                   
                
    <interceptor-stack name="executeAndWaitStack">   
                    
    <interceptor-ref name="execAndWait">   
                        
    <param name="excludeMethods">input,back,cancel</param>   
                    
    </interceptor-ref>   
                    
    <interceptor-ref name="defaultStack"/>   
                    
    <interceptor-ref name="execAndWait">   
                        
    <param name="excludeMethods">input,back,cancel</param>   
                    
    </interceptor-ref>   
                
    </interceptor-stack>   
      
                   
                
    <interceptor name="external-ref" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>   
                
    <interceptor name="model-driven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>   
                
    <interceptor name="static-params" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>   
                
    <interceptor name="scoped-model-driven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>   
                
    <interceptor name="servlet-config" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>   
                
    <interceptor name="token-session" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>   
      
           
    </interceptors>   
    <!--定義默認攔截器為"defaultStack"-->   
            
    <default-interceptor-ref name="defaultStack"/>   


    主站蜘蛛池模板: 亚洲AV无码乱码在线观看代蜜桃 | 两个人看的www免费视频中文| 99999久久久久久亚洲| 亚洲乱码中文字幕综合| 国产午夜鲁丝片AV无码免费| 亚欧色视频在线观看免费| a在线免费观看视频| 成年网站免费入口在线观看| 亚洲乱码日产精品一二三| 亚洲成年人电影网站| 亚洲av中文无码乱人伦在线r▽ | 亚洲熟女精品中文字幕| 亚洲最大黄色网站| 亚洲午夜视频在线观看| 国产精品亚洲аv无码播放| 亚洲欧洲一区二区三区| 免费大片黄手机在线观看| 免费精品一区二区三区在线观看| 日韩欧毛片免费视频| 免费人成在线观看69式小视频| 久久久久国产免费| 国产成人精品无码免费看| 九九精品成人免费国产片| 9i9精品国产免费久久| 久久精品免费网站网| 一本一道dvd在线观看免费视频| 美女视频黄频a免费大全视频| 久久久久久久久无码精品亚洲日韩| 国产成人亚洲综合一区| 国产亚洲福利在线视频| 亚洲欧美一区二区三区日产| 亚洲综合在线一区二区三区| jiz zz在亚洲| 日本亚洲欧美色视频在线播放| 亚洲av无码成人影院一区 | 国产免费av片在线无码免费看| 国产午夜影视大全免费观看| 免费一级一片一毛片| av在线亚洲欧洲日产一区二区| 亚洲综合色婷婷七月丁香| 久久亚洲AV午夜福利精品一区|