shenang博客技術(shù)文檔


          理論不懂就實(shí)踐,實(shí)踐不會就學(xué)理論!

          posts - 35,comments - 55,trackbacks - 0

          一.理解攔截器

             1. 攔截器是在防問某個(gè)方法,字段之前或之后實(shí)施攔截,并且攔截器是可插拔的,攔截器是AOP的一種實(shí)現(xiàn).

             2. 攔截器棧(Interceptor Stack)。攔截器棧就是將攔截器按一定的順序聯(lián)結(jié)成一條鏈。在訪問被攔截的方法或字段時(shí),       攔截器鏈中的攔截器就會按其之前定義的順序被調(diào)用。

          二.實(shí)現(xiàn)原理
              Struts2攔截器的實(shí)現(xiàn)原理相對簡單,當(dāng)請求struts2的action時(shí),Struts 2會查找配置文件,并根據(jù)其配置實(shí)例化相對的    攔截器對象,然后串成一個(gè)列表,最后一個(gè)一個(gè)地調(diào)用列表中的攔截器

          三.攔截器的配置
            1. 普通的攔截器

          xml代碼(即stuts的配置文件)
           1 <package name="default" extends="struts-default"><BR>  
           2   
           3   
           4    <interceptors><BR>  
           5   
           6   
           7        <interceptor name="攔截器名1" class="攔截器實(shí)現(xiàn)類"/><BR>  
           8   
           9   
          10        <interceptor name="攔截器名2" class="攔截器實(shí)現(xiàn)類"/><BR>  
          11   
          12   
          13    </interceptors><BR>  
          14   
          15   
          16 <action name="login" class="com.Logon"><BR>  
          17   
          18   
          19   <interceptor-ref name="defaultStack"/><BR>  
          20   
          21   
          22         <interceptor-ref name="攔截器名1"/><BR>  
          23   
          24   
          25         <interceptor-ref name="攔截器名2"/><BR>  
          26   
          27   
          28          <result name="input">logon.jsp</result><BR>  
          29   
          30   
          31          <result name="success">/index.jsp</result><BR>  
          32   
          33   
          34    </action><BR>  
          35   
          36   
          37 </package> 

          2. 攔截器棧
           1 package name="default" extends="struts-default"><BR>  
           2   
           3   
           4    <interceptors><BR>  
           5   
           6   
           7         <interceptor name="攔截器名1" class="攔截器實(shí)現(xiàn)類"/><BR>  
           8   
           9   
          10         <interceptor name="攔截器名2" class="攔截器實(shí)現(xiàn)類"/><BR>  
          11   
          12   
          13         <interceptor-stack name="myStack"><BR>  
          14   
          15   
          16            <interceptor-ref name="攔截器名1"/><BR>  
          17   
          18   
          19            <interceptor-ref name="攔截器名2"/><BR>  
          20   
          21   
          22         </interceptor-stack><BR>  
          23   
          24   
          25     </interceptors><BR>  
          26   
          27   
          28     <action name="login" class="com.Logon"><BR>  
          29   
          30   
          31      <interceptor-ref name="defaultStack"/><BR>  
          32   
          33   
          34          <interceptor-ref name="myStack"/><BR>  
          35   
          36   
          37          <result name="input">login.jsp</result><BR>  
          38   
          39   
          40          <resultnameresultname="success" >/index.jsp</result><BR>  
          41   
          42   
          43 </action><BR>  
          44   
          45   
          46 </package><BR> 

          需要注意的是,如果為Action指定了一個(gè)攔截器,則系統(tǒng)默認(rèn)的攔截器棧將會失去作用。為了繼續(xù)使用默認(rèn)攔截器,所以上面配置文件中手動引入了默認(rèn)攔截器.

          四.自定義攔截器
             1.直接或間接實(shí)現(xiàn)接口com.opensymphony.xwork2.interceptor.Interceptor。
             2.或者繼承類com.opensymphony.xwork2.interceptor.AbstractInterceptor
             3.通過<interceptor>元素來定義攔截器
             4.通過<interceptor-ref>元素來使用攔截器

          五.使用攔截器實(shí)現(xiàn)權(quán)限控制
            1.功能
              使用自定義攔截器來完成用戶權(quán)限的控制,當(dāng)執(zhí)行操作時(shí),判斷用戶是否己經(jīng)登陸,如果沒有登陸跳轉(zhuǎn)到登陸頁面

            2.攔截器類

          java代碼
           1 package com.interceptor;   
           2 <BR>
             
           3 <BR>import
           com.opensymphony.xwork2.Action;   
           4 <BR>import
           com.opensymphony.xwork2.ActionContext;   
           5 <BR>import
           com.opensymphony.xwork2.ActionInvocation;   
           6 <BR>import
           com.opensymphony.xwork2.interceptor.AbstractInterceptor;   
           7 <BR>
             
           8 <BR>public class AuthorizationInterceptor extends
           AbstractInterceptor {   
           9 <BR>
             
          10 <BR>    private static final long serialVersionUID = 1L
          ;   
          11 <BR>
             
          12 <BR>
              @Override  
          13 <BR>    public String intercept(ActionInvocation actionInvocation) throws
           Exception {   
          14 <BR>
                     
          15 <BR>        ActionContext actionContext =
           actionInvocation.getInvocationContext();   
          16 <BR>
                     
          17 <BR>        Object user = actionContext.get("user"
          );   
          18 <BR>
                     
          19 <BR>        if(user != null
          ){   
          20 <BR>            return
           actionInvocation.invoke();   
          21 <BR>        } else
          {   
          22 <BR>            actionInvocation.getInvocationContext().put("nav_title""你還沒有登陸,請先登陸"
          );   
          23 <BR>            return
           Action.LOGIN;   
          24 <BR>
                  }   
          25 <BR>
              }   
          26 <BR>
             
          27 <BR>
          }   
          28 <BR>  

          2.配置
          Xml代碼
           1 <?xml version="1.0" encoding="UTF-8" ?><BR>  
           2 
            
           3 
            
           4 <!DOCTYPE struts PUBLIC<BR>
            
           5 
            
           6 
            
           7     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<BR>
            
           8 
            
           9 
            
          10     "http://struts.apache.org/dtds/struts-2.0.dtd"><BR>
            
          11 
            
          12 <struts>  <BR>
            
          13    <package name="user" extends="struts-default">
            
          14 
            
          15         <interceptors>
            
          16             <!-- 定義攔截器  -->
            
          17             <interceptor name="authority" class="com.interceptor.AuthorizationInterceptor"></interceptor>
            
          18         </interceptors><BR>
            
          19 
            
          20         <action name="user" class="com.UserAction"><BR>
            
          21             <!-- 使用攔截器 -->
            
          22             <interceptor-ref name="authority"/>
            
          23             <interceptor-ref name="defaultStack"/>
            
          24             <result name="succee">/logon/welcome.jsp</result><BR>
            
          25             <result name="login">/logon/logon.jsp</result>
            
          26         </action>
            
          27     </package>
            
          28 </struts><BR> 

           訪問http://localhost:8080/struts2-interceptor/user.action時(shí),會判斷用戶是否登陸


          六.方法攔截器
            1.Struts2提供MethodFilterInterceptor類,該類是AbstractInerceptor的子類,可以實(shí)現(xiàn)對Action方法的攔截.
            2. MethodFilterInterceptor中有兩個(gè)方法
             setExcludeMethods:排除需要過濾的方法
               setIncludeMethods:設(shè)置需要過濾的方法
               如果一個(gè)方法同時(shí)在excludeMethods和includeMethods中出現(xiàn),則會被攔截

            3.實(shí)現(xiàn)攔截器
          Java代碼

           1 package com.interceptor;   
           2 <BR>   
           3 <BR>import com.opensymphony.xwork2.ActionInvocation;   
           4 <BR>import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;   
           5 <BR>   
           6 <BR>public class LogInterceptor extends MethodFilterInterceptor {   
           7 <BR>   
           8 <BR>    private static final long serialVersionUID = 1L;   
           9 <BR>   
          10 <BR>    private String name;   
          11 <BR>       
          12 <BR>    @Override  
          13 <BR>    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {   
          14 <BR>       
          15 <BR>        System.out.println("攔截器名稱:" + name);   
          16 <BR>        System.out.println("action:" + actionInvocation.getAction());   
          17 <BR>           
          18 <BR>        return actionInvocation.invoke();   
          19 <BR>    }   
          20 <BR>   
          21 <BR>    public String getName() {   
          22 <BR>        return name;   
          23 <BR>    }   
          24 <BR>   
          25 <BR>    public void setName(String name) {   
          26 <BR>        this.name = name;   
          27 <BR>    }   
          28 <BR>}   
          29 <BR> 
          Java代碼
           1 package com;<BR><BR>   
           2 public class ManageAction {<BR><BR>   
           3     public String execute(){   
           4         System.out.println("execute.");<BR>   
           5         return "succee";   
           6     }<BR>   
           7   
           8     public String search(){<BR>   
           9         System.out.println("search.");   
          10         return "succee";   
          11     }   
          12     public String add(){   
          13         System.out.println("add.");   
          14         return "succee";   
          15     }   
          16 }<BR>  
          17 

          struts.xml配置
           1 <action name="manage" class="com.ManageAction"><BR>  
           2             <interceptor-ref name="log"><BR>  
           3                 <param name="name">日志攔截</param><BR>  
           4                 <!-- 設(shè)置需要攔截的方法,指定多個(gè)方法以逗號隔開 -->  
           5   
           6                 <param name="includeMethods">execute,add</param><BR>  
           7                 <!-- 設(shè)置不需要攔截的方法,execute在includeMethods中同時(shí)存在,execute會被攔截 --><BR>  
           8                 <param name="excludeMethods">search,execute</param>  
           9             </interceptor-ref><BR>  
          10             <result name="succee">/welcome.jsp</result>  
          11          </action>  
          12 

          打開瀏覽器訪問 http://localhost:8080/struts2-interceptor/manage.action


          會報(bào)執(zhí)行execute方法,會執(zhí)行攔截器
          攔截器名稱:日志攔截
          action:com.ManageAction@1a0ae6d
          execute....

          當(dāng)訪問 http://localhost:8080/struts2-interceptor/manage!search.action
          執(zhí)行search方法,不會執(zhí)行攔截器


          posted on 2009-04-07 11:12 重慶理工小子 閱讀(381) 評論(0)  編輯  收藏 所屬分類: Struts2

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 海城市| 开平市| 固原市| 华池县| 潼南县| 册亨县| 台湾省| 信丰县| 彰武县| 介休市| 铜山县| 西城区| 项城市| 嵊泗县| 旅游| 石嘴山市| 东安县| 南丰县| 丹阳市| 独山县| 宜黄县| 仁寿县| 沧州市| 灵璧县| 奇台县| 大安市| 中山市| 甘洛县| 泰宁县| 安徽省| 上蔡县| 佛山市| 黎平县| 仁寿县| 惠来县| 夏津县| 株洲市| 闽清县| 新民市| 明水县| 浑源县|