隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創(chuàng)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

          常用鏈接

          常用設(shè)置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156396
          • 排名 - 390

          最新評(píng)論

          [標(biāo)題]:[原]Struts2-攔截器
          [時(shí)間]:2009-7-31
          [摘要]:Struts Interceptor
          [關(guān)鍵字]:浪曦視頻,Struts2應(yīng)用開發(fā)系列,WebWork,Apache,Interceptor,攔截器,動(dòng)態(tài)代理,Java,Proxy
          [環(huán)境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
          [作者]:Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys

          [正文]:
          1、知識(shí)點(diǎn)
          a.Struts2攔截器Interceptor
              Struts2攔截器的根接口為: xwork-2.1.2.jar/com.opensymphony.xwork2.interceptor.Interceptor
              自定義的攔截器可以實(shí)現(xiàn)Interceptor接口。com.opensymphony.xwork2.interceptor.AbstractInterceptor 提供了對(duì)Interceptor的默認(rèn)實(shí)現(xiàn),自定義攔截器也可以繼承AbstractInterceptor。

          b.定義攔截器
          /StrutsHelloWorld/src/wintys/struts2/interceptor/MyInterceptor.java:
          package wintys.struts2.interceptor;

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

          /**
           * 攔截器、Interceptor
           *
           * http://www.aygfsteel.com/wintys
           * @author Winty (wintys@gmail.com)
           * @version 2009-07-30
           *
           */
          @SuppressWarnings("serial")
          public class MyInterceptor implements Interceptor {

              @Override
              public String intercept(ActionInvocation invocation) throws Exception {
                  
                  System.out.println("before...");
                  
                  String result = invocation.invoke();
                  
                  System.out.println("after...");
                  
                  return result;
              }

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

              @Override
              public void init() {
                  System.out.println("MyInterceptor init()...");
              }

          }

          c.在struts.xml中配置攔截器
          <package name="MyStruts" extends="struts-default">
              <interceptors>
                      <interceptor name="myInterceptor" class="wintys.struts2.interceptor.MyInterceptor" />

                      <interceptor-stack name="myStack">
                          <interceptor-ref name="myInterceptor"/>
                          <interceptor-ref name="defaultStack"/>
                      </interceptor-stack>
              </interceptors>
              ......
              <action name="intercept" class="wintys.struts2.interceptor.InterceptAction">
                  <result name="success">/interceptor/output.jsp</result>
                     <result name="input">/interceptor/input.jsp</result>
                  <interceptor-ref name="myStack" />
              </action>
          </package>

              defaultStack是Struts默認(rèn)的攔截器。在action中手動(dòng)引入interceptor后,就不會(huì)啟用默認(rèn)的interceptor,除非手動(dòng)引入。所以要加上默認(rèn)interceptor:<interceptor-ref name="defaultStack">。

              在input.jsp請(qǐng)求Action "intercept"時(shí),在Action的execute()方法執(zhí)行時(shí),就會(huì)觸發(fā)攔截器。

          d.帶參數(shù)的攔截器
              配置(在定義時(shí)給參數(shù)賦值):
              <interceptor name="myParamInterceptor" class="wintys.struts2.interceptor.MyParamInterceptor" >
                  <param name="info">This is a param.</param>
              </interceptor>

              在自定義攔截器中實(shí)現(xiàn)代碼(添加info屬性、setter和getter):
          /StrutsHelloWorld/src/wintys/struts2/interceptor/MyParamInterceptor.java
          package wintys.struts2.interceptor;

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

          /**
           * 帶參數(shù)的攔截器、AbstractInterceptor
           *
           * http://www.aygfsteel.com/wintys
           * @author Winty (wintys@gmail.com)
           * @version 2009-07-30
           *
           */
          @SuppressWarnings("serial")
          public class MyParamInterceptor extends AbstractInterceptor {
              private String info;
              
              public String getInfo() {
                  return info;
              }

              public void setInfo(String info) {
                  this.info = info;
              }

              @Override
              public String intercept(ActionInvocation invocation) throws Exception {
                  System.out.println("before2...");
                  System.out.println("info:" + info);
                  
                  String result = invocation.invoke();
                  
                  System.out.println("after2...");
                  
                  return result;
              }

          }

              在引用時(shí)給參數(shù)賦值,會(huì)覆蓋定義時(shí)的賦值:
              <interceptor-ref name="myParamInterceptor">
                  <param name="info">This is another param.</param>
              </interceptor-ref>


          e.攔截器棧
              攔截器棧與攔截器具有同等地位,使用相同。攔截器棧可以再包含攔截器或攔截器棧。
          <interceptors>
              <interceptor-stack name="myStack">
                  <interceptor-ref name="myInterceptor" />
                  <interceptor-ref name="defaultStack" />
              </interceptor>
          </interceptors>

              攔截器棧中定義的多個(gè)攔截器執(zhí)行順序與攔截器配置順序相同。同時(shí),多個(gè)攔截器的執(zhí)行流程如下: interceptorA begin => interceptorB begin => action => interceptorB end => interceptorA end


          f.指定默認(rèn)攔截器    
              Struts默認(rèn)的攔截器是defaultStack,可以在struts.xml中使用如下配置重新指定默認(rèn)攔截器:
          <package>
              ......
              <default-interceptor-ref name="myStack" />
              ......
          </package>


          g.方法過濾攔截器MethodFilterInteceptor
              MethodFilterInteceptor可以選擇需要過濾的方法,通過參數(shù)進(jìn)行配置。實(shí)現(xiàn)MethodFilterInteceptor.doIntercept(),以提供攔截功能。
          <action>
              <interceptor-ref name="myInterceptor">
                  <param name="includeMethods">test,execute</param>
                  <param name="excludeMethods">somemethod</param>
              </interceptor-ref>
          </action>


          f.PreResultListener
              可以在攔截器中添加PreResultListener,以實(shí)現(xiàn)特定功能。PreResultListener在業(yè)務(wù)方法(通常為execute)返回后(執(zhí)行成功則返回"success"),頁面視圖呈現(xiàn)到客戶端之前執(zhí)行。

              public String intercept(ActionInvocation invocation) throws Exception {
                  ......
                  invocation.addPreResultListener(...);
                  ......
              }


          2、詳細(xì)代碼
          /StrutsHelloWorld/src/wintys/struts2/interceptor/MyMethodFilterInterceptor.java:
          package wintys.struts2.interceptor;

          import com.opensymphony.xwork2.ActionInvocation;
          import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
          import com.opensymphony.xwork2.interceptor.PreResultListener;

          /**
           * 選擇性攔截方法的攔截器MethodFilterInterceptor、監(jiān)聽器PreResultListener
           *
           * http://www.aygfsteel.com/wintys
           * @author Winty (wintys@gmail.com)
           * @version 2009-07-30
           *
           */
          @SuppressWarnings("serial")
          public class MyMethodFilterInterceptor extends MethodFilterInterceptor {

              @Override
              protected String doIntercept(ActionInvocation invocation) throws Exception {
                  System.out.println("MyMethodFilterInterceptor is running...");
                  
                  //添加監(jiān)聽器PreResultListener
                  invocation.addPreResultListener(new PreResultListener(){
                      public void beforeResult(ActionInvocation invocation, String resultCode){
                          System.out.println("PreResultListener ..." + resultCode);
                      }
                      
                  });
                  
                  String result = invocation.invoke();        
                  
                  return result;
              }

          }


          /StrutsHelloWorld/WebRoot/interceptor/input.jsp:
          <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
          <%@ taglib uri="/struts-tags" prefix="s" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            <head>
              <title></title>
            </head>
           
            <body>
            <s:form action="intercept">
                <s:textfield name="user" label="user" /><br/>
                <s:submit name="submit" label=" 提交  " />
            </s:form>
              
            </body>
          </html>



          /StrutsHelloWorld/src/wintys/struts2/interceptor/InterceptAction.java:
          package wintys.struts2.interceptor;

          import com.opensymphony.xwork2.ActionSupport;

          /**
           *
           * @author Winty (wintys@gmail.com)
           * @version 2009-07-30
           * http://www.aygfsteel.com/wintys
           */
          @SuppressWarnings("serial")
          public class InterceptAction extends ActionSupport {
              private String user;

              public String getUser() {
                  return user;
              }

              public void setUser(String user) {
                  this.user = user;
              }
              
              @Override
              public String execute() throws Exception {
                  System.out.println("execute()...");
                  
                  return SUCCESS;
              }
              
              @Override
              public void validate() {
                  System.out.println("validate()...");
                  
                  if(user == null || user.length() < 6){
                      addFieldError("user", "invalid user");
                  }
              }
          }



          /StrutsHelloWorld/WebRoot/interceptor/output.jsp:
          <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
          <%@ taglib uri="/struts-tags" prefix="s" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            <head>
              <title>My JSP 'output.jsp' starting page</title>    
           </head>
           
            <body>
              <s:property value="user"/>
            </body>
          </html>


          /StrutsHelloWorld/src/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="MyStruts" extends="struts-default">
                  <!-- 攔截器 -->
                  <interceptors>
                      <interceptor name="myInterceptor" class="wintys.struts2.interceptor.MyInterceptor" />
                      <interceptor name="myParamInterceptor" class="wintys.struts2.interceptor.MyParamInterceptor" >
                          <param name="info">This is a param.</param>
                      </interceptor>
                      <interceptor name="myMethodFilterInterceptor" class="wintys.struts2.interceptor.MyMethodFilterInterceptor">
                          <param name="includeMethods">execute</param>
                      </interceptor>
                      
                      <interceptor-stack name="myStack">
                          <interceptor-ref name="myParamInterceptor">
                              <param name="info">This is another param.</param>
                          </interceptor-ref>
                          <interceptor-ref name="myInterceptor"/>
                          <interceptor-ref name="myMethodFilterInterceptor"/>
                          <interceptor-ref name="defaultStack"/>
                      </interceptor-stack>
                  </interceptors>
                        
                  <action name="intercept" class="wintys.struts2.interceptor.InterceptAction">
                      <result name="success">/interceptor/output.jsp</result>
                         <result name="input">/interceptor/input.jsp</result>
                         <interceptor-ref name="myStack" />
                  </action>
                  
              </package>
          </struts>

          3、小例子:登錄攔截
              登錄攔截:攔截Action實(shí)現(xiàn)在輸入信息之前必須登錄,如果沒有登錄,則轉(zhuǎn)到登錄頁面。

          /StrutsHelloWorld/WebRoot/interceptor/login.jsp:
          <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
          <%@ taglib uri="/struts-tags" prefix="s" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            <head>
              <title></title>
            </head>
           
            <body>
            <s:form action="authentication">
              正確id:10000
                <s:textfield name="id" label="id" /><br/>
                <s:submit name="submit" label=" 提交  " />
            </s:form>
              
            </body>
          </html>

          Struts2中的Session可以脫離容器,以方便測(cè)試,對(duì)應(yīng)于容器中的Session。

          /StrutsHelloWorld/src/wintys/struts2/interceptor/AuthenticationAction.java:
          package wintys.struts2.interceptor;

          import java.util.Map;

          import com.opensymphony.xwork2.ActionContext;
          import com.opensymphony.xwork2.ActionSupport;

          @SuppressWarnings("serial")
          public class AuthenticationAction extends ActionSupport {
              private String id;

              public String getId() {
                  return id;
              }

              public void setId(String id) {
                  this.id = id;
              }
              
              @Override
              public String execute() throws Exception {
                  
                  if( id.equals("10000")){
                      Map<String , Object> map = ActionContext.getContext().getSession();
                      map.put("id", id);
                      
                      return SUCCESS;
                  }
                  else{
                      addFieldError("id", "id error");
                      
                      return INPUT;
                  }
              }
              
              @Override
              public void validate() {
                  if(id == null || id.length() < 3)
                      addFieldError("id", "invalid id");
              }
          }


          /StrutsHelloWorld/src/wintys/struts2/interceptor/AuthenticationInterceptor.java:
          package wintys.struts2.interceptor;

          import java.util.Map;

          import com.opensymphony.xwork2.Action;
          import com.opensymphony.xwork2.ActionInvocation;
          import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

          /**
           * 認(rèn)證攔截器:判斷用戶是否已登錄
           *
           * http://www.aygfsteel.com/wintys
           * @author Winty (wintys@gmail.com)
           * @version 2009-07-30
           *
           */
          @SuppressWarnings("serial")
          public class AuthenticationInterceptor extends AbstractInterceptor {

              @Override
              public String intercept(ActionInvocation invocation) throws Exception {
                  System.out.println("AuthenticationInterceptor ...");
                  
                  Map<String , Object> map = invocation.getInvocationContext().getSession();
                  
                  Object id = map.get("id");
                  if( id == null){
                      return Action.LOGIN;
                  }else{
                      System.out.println("id" + id);
                      
                      return invocation.invoke();
                  }
              }

          }


          struts.xml配置:
          ......
          <package name="MyStruts" extends="struts-default">
                  <!-- 攔截器 -->
                  <interceptors>
                      <interceptor name="authenticationInterceptor" class="wintys.struts2.interceptor.AuthenticationInterceptor"/>
                      
                      <interceptor-stack name="myStack">
                          <interceptor-ref name="authenticationInterceptor"/>
                          <interceptor-ref name="defaultStack"/>
                      </interceptor-stack>
                  </interceptors>
                  
                  <!-- 全局result -->
                  <global-results>
                      <result name="login" type="redirect">/interceptor/login.jsp</result>
                  </global-results>
                  
                  <action name="intercept" class="wintys.struts2.interceptor.InterceptAction">
                      <result name="success">/interceptor/output.jsp</result>
                         <result name="input">/interceptor/input.jsp</result>
                         <interceptor-ref name="myStack" />
                  </action>
                  <action name="authentication" class="wintys.struts2.interceptor.AuthenticationAction">
                      <result name="success">/interceptor/input.jsp</result>
                      <result name="input" type="dispatcher">/interceptor/login.jsp</result>
                  </action>
                  
          </package>
          ......

          [參考資料]:
              《浪曦視頻之Struts2應(yīng)用開發(fā)系列》

          [附件]:
              源代碼: http://www.aygfsteel.com/Files/wintys/struts_HelloWorld_Interceptor.zip
          posted on 2009-08-01 00:21 天堂露珠 閱讀(766) 評(píng)論(0)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 内乡县| 乌恰县| 文水县| 中江县| 西林县| 铁岭市| 南皮县| 曲靖市| 祁门县| 武冈市| 宕昌县| 滦平县| 奉化市| 汝州市| 北流市| 嘉鱼县| 雷州市| 北京市| 京山县| 镇沅| 汉阴县| 绥中县| 子洲县| 日喀则市| 彭州市| 星子县| 依安县| 亚东县| 南丰县| 利津县| 琼结县| 宿迁市| 宁明县| 吴江市| 平凉市| 鱼台县| 江孜县| 武强县| 金华市| 封丘县| 太康县|