fun

           

          10分鐘學(xué)懂Struts 2.0 攔截器

          Struts 2.0攔截器

          簡(jiǎn)介

           

          Struts 2.0 中的攔截器,要實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor接口,在struts.xml中配置。可以用攔截器來完成調(diào)用Action業(yè)務(wù)邏輯之前的預(yù)處理或是之后的善后處理。還可以通過配置多個(gè)攔截器來滿足action需求。

           

          Interceptor stack是由多個(gè)攔截器組成的攔截器組,在攔截器組中可以對(duì)每一個(gè)攔截器映射。所有進(jìn)行配置攔截器時(shí),不必對(duì)每一個(gè)攔截器進(jìn)行配置,而只需對(duì)interceptor stack進(jìn)行配置即可。在struts 2中默認(rèn)配置了一個(gè)全局interceptor stack,包括Exception InterceptorValidation Interceptor等。

           

          實(shí)例

           

          在這個(gè)實(shí)例當(dāng)中,我將配置一個(gè)時(shí)間攔截器,用來統(tǒng)計(jì)每個(gè)action的請(qǐng)求時(shí)間。

          package interceptor;      
               
          import com.opensymphony.xwork2.ActionInvocation;      
          import com.opensymphony.xwork2.interceptor.Interceptor;      
          /**
          *author by 
          http://www.bt285.cn http://www.5a520.cn
          */
               
          public class ActionTimer implements Interceptor{      
              
          public String intercept(ActionInvocation next) throws Exception {      
                  
          long t1 = System.currentTimeMillis();      
                  String s
          = next.invoke();      
                  
          long t2 = System.currentTimeMillis();      
                  System.out.println(
          "Action "+next.getAction().getClass().getName()+" took "+(t2-t1)+" millisecs");      
                  
          return s;      
              }
                
                    
              
          public void init() {      
              }
                
              
          public void destroy() {      
              }
                
          }
            
          struts.xml
          <?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="interceptor" extends="struts-default">     
                  
          <interceptors>     
                      
          <interceptor name="actiontimer"     
                          class
          ="interceptor.ActionTimer" />     
               
                      
          <interceptor-stack name="demostack">     
                          
          <interceptor-ref name="defaultStack" />     
                          
          <interceptor-ref name="actiontimer" />     
                      
          </interceptor-stack>     
                  
          </interceptors>     
                  
          <default-interceptor-ref name="demostack" />     
                  
          <action name="InterceptorDemo"     
                      class
          ="interceptor.action.InterceptorDemo">     
                      
          <result>http://www.bt285.cn /interceptor/interceptordemo.jsp</result>     
                  
          </action>     
              
          </package>     
               
          </struts>   

          interceptordemo.jsp

          <html>     
          <head>     
               
          </head>     
          <body>     
          </body>     
          </html>   

           

           

          posted on 2009-05-08 20:31 fun 閱讀(1520) 評(píng)論(0)  編輯  收藏

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(11)

          隨筆檔案

          友情鏈接

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 南雄市| 乌恰县| 永顺县| 黄山市| 博湖县| 定远县| 葵青区| 成武县| 鹿泉市| 阿巴嘎旗| 佛坪县| 灌南县| 安远县| 耒阳市| 怀集县| 苍溪县| 旺苍县| 黄平县| 武隆县| 隆德县| 景洪市| 来凤县| 泗水县| 石楼县| 华阴市| 玛多县| 阳西县| 香河县| 井研县| 奉贤区| 保山市| 卢湾区| 兴义市| 黔东| 于都县| 贵港市| 中卫市| 游戏| 延寿县| 红安县| 枝江市|