Struts2 自定義攔截器(方法攔截器)


struts2系統自帶了很多攔截器,有時需要我們自己定義,一般有兩種方式:

 一、實現Interceptor接口 

1 public interface Interceptor extends Serializable{    

2      public void init();    

3      public void destroy();    

4      public String intercept(ActionInvocation invocation)();    

5 } 
 
        

並實現上述方法

 二、繼承AbstractInterceptor類,重寫intercept()方法即可 此方法更可行,其實AbstractInterceptor類也就是實現了Interceptor接口
1 invocation.invoke();表示該方法執行完后執行Action的execute()方法或者執行下一個攔截器    

2 invocation.getAction(); 可以將該法強制轉換為Action的類類型 
 
        
 三、方法攔截器:繼承MethodFilterInterceptor類,重寫doIntercept()方法 MethodFilerInterceptor實現方法過濾中用到的兩個參數 execludeMethods:該參數指定攔截器拒絕攔截的方法列表,多個方法用“,”隔開支持通配符*,例如add*,表示所有以add開頭的方法),如果指定了這個參數攔截器不會攔截指定列表中的方法,就是所謂的黑名單 includeMethods: 該參數指定攔截器需要攔截的方法列表,多個方法用“,”隔開支持通配符*,例如add*,表示所有以add開頭的方法),如果指定了參數,則指定的Action在執行前會被攔截,即白名單。 定義好自定義攔截器后,就要使用自定義攔截器,在struts.xml文檔中 一、包內定義攔截器 
1 <package....>    

2      <interceptors>    

3           <interceptor name="myinterceptor" class="....">    

4           </interceptor>    

5      </interceptors>    

6 </package>
 
        

 

二、action內使用攔截器
1 <action .....>    

2      <result.....></result>    

3      <interceptor-ref name="defaultStack"></interceptor-ref>    

4      <interceptor-ref name="myinterceptor"></interceptor-ref>    

5 </action> 
 
        
 主要:可以看出使用了自定義攔截器的action要配置默認攔截器的引用,因為默認攔截器包含了參數的讀取、session的管理等功能 以下是例子: MyMethodInterceptor類
01 public class MyMethodInterceptor extends MethodFilterInterceptor{    
02     protected String doIntercept(ActionInvocation invocation) throws Exception {    
03         // TODO Auto-generated method stub    
04         System.out.println("進入MyMethodInterceptor方法攔截器!!!!!!!!!!!!!");    
05         Map session = invocation.getInvocationContext().getSession();    
06         String name = (String) session.get("uname");    
07         if (name != null) {    
08             return invocation.invoke();    
09         }    
10         return "input";    
11     }    
12 } 
 
        
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!-- <constant name="struts.action.extension" value="action,abc"></constant>-->

     <package abstract="true" name="pk1" namespace="/" extends="struts-default"></package>
     <package name="k2" extends="pk1">

        <interceptors>       
             <interceptor name="method1" class="org.interceptors.MyMethodInterceptor">
                <param name="excludeMethods">login</param>
            </interceptor>
<!--定義攔截器棧-->
             <interceptor-stack name="myStack">
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="method1"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

<!--設置默認攔截器-->
<!-- <default-interceptor-ref name="myStack"></default-interceptor-ref>-->

         <action name="login_*_*" class="org.hzy.Actions.LoginAction" method="{1}">
            <interceptor-ref name="myStack"></interceptor-ref>
            <result name="success" type="chain">{2}</result>
            <result name="input">index.jsp</result>
            <result name="error">/WEB-INF/Jsp/error.jsp</result>
        </action>
         <action name="query_*" class="org.hzy.Actions.QueryAction" method="{1}">
            <result>/WEB-INF/Jsp/show.jsp</result>
        </action>
         <action name="upload_*" class="org.hzy.Actions.FileUploadAction" method="{1}">
            <result>/WEB-INF/Jsp/show.jsp</result>
        </action>
    </package>
</struts>
 
         
         
        

 

 相關:Struts2攔截器的使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM