shenang博客技術文檔


          理論不懂就實踐,實踐不會就學理論!

          posts - 35,comments - 55,trackbacks - 0

          一.理解攔截器

             1. 攔截器是在防問某個方法,字段之前或之后實施攔截,并且攔截器是可插拔的,攔截器是AOP的一種實現.

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

          二.實現原理
              Struts2攔截器的實現原理相對簡單,當請求struts2的action時,Struts 2會查找配置文件,并根據其配置實例化相對的    攔截器對象,然后串成一個列表,最后一個一個地調用列表中的攔截器

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

          xml代碼(即stuts的配置文件)
           1 <package name="default" extends="struts-default"><BR>  
           2   
           3   
           4    <interceptors><BR>  
           5   
           6   
           7        <interceptor name="攔截器名1" class="攔截器實現類"/><BR>  
           8   
           9   
          10        <interceptor name="攔截器名2" class="攔截器實現類"/><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="攔截器實現類"/><BR>  
           8   
           9   
          10         <interceptor name="攔截器名2" class="攔截器實現類"/><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指定了一個攔截器,則系統默認的攔截器棧將會失去作用。為了繼續使用默認攔截器,所以上面配置文件中手動引入了默認攔截器.

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

          五.使用攔截器實現權限控制
            1.功能
              使用自定義攔截器來完成用戶權限的控制,當執行操作時,判斷用戶是否己經登陸,如果沒有登陸跳轉到登陸頁面

            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時,會判斷用戶是否登陸


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

            3.實現攔截器
          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                 <!-- 設置需要攔截的方法,指定多個方法以逗號隔開 -->  
           5   
           6                 <param name="includeMethods">execute,add</param><BR>  
           7                 <!-- 設置不需要攔截的方法,execute在includeMethods中同時存在,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


          會報執行execute方法,會執行攔截器
          攔截器名稱:日志攔截
          action:com.ManageAction@1a0ae6d
          execute....

          當訪問 http://localhost:8080/struts2-interceptor/manage!search.action
          執行search方法,不會執行攔截器


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

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


          網站導航:
           
          主站蜘蛛池模板: 新昌县| 莱西市| 勐海县| 朝阳市| 台南市| 望都县| 康马县| 敖汉旗| 平山县| 民勤县| 大埔区| 和平县| 商水县| 稷山县| 九龙县| 临安市| 神池县| 大冶市| 柳林县| 田东县| 阜康市| 新干县| 正安县| 山西省| 万安县| 饶平县| 阿图什市| 南江县| 仙桃市| 长子县| 南岸区| 金湖县| 遂平县| 延边| 红原县| 紫阳县| 深泽县| 淮安市| 韶关市| 兴安县| 桐柏县|