歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2基礎——自定義攔截器

Struts2基礎——自定義攔截器

日期:2017/3/1 9:14:23   编辑:Linux編程

一、自定義攔截器

默認的攔截器能實現的功能是有限的,Struts2 支持自定義攔截器。

二、攔截器類

1.實現 Interceptor 接口

2.繼承 AbstractInterceptor 抽象類,需要實現 public String intercept(ActionInvocation actionInvocation) 方法,其中通過 actionInvocation.invoke() 繼續調用後續攔截器 和 Action 方法。

Struts2 會自動跳轉到自定義攔截器的 interceptor 方法返回值對應的 result,如果直接返回一個 String,那麼會將控制器交給目標 action 對應的 result。

3.注冊自定義攔截器與使用

(1)Action 級

<package name="default" namespace="/" extends="struts-default">
  <interceptor name="myInterceptor" class="com.nucsoft.struts.interceptor.MyInterceptor"/>
  <action name="interceptor" class="com.nucsoft.struts.token.InterceptorAction">
    <interceptor-ref name="myInterceptor"/>
    <interceptor-ref name="defaultStack"/>
      <result>/success.jsp</result>
      <result name="input">/error.jsp</result>
  </action>
</package>

(2)package 級

<package name="default" namespace="/" extends="struts-default">
  <interceptors>
    <interceptor name="myInterceptor" class="com.nucsoft.struts.interceptor.MyInterceptor"/>
    <interceptor-stack name="myInterceptorStack">
      <interceptor-ref name="myInterceptor"/>
      <interceptor-ref name="defaultStack"/>
    </interceptor-stack>
  </interceptors>
  <default-interceptor-ref name="myInterceptorStack"/>
  <action name="myInterceptor" class="com.nucsoft.struts.token.InterceptorAction" method="myInterceptor">
    <result>/success.jsp</result>
    <result name="input">/error.jsp</result>
  </action>
</package>

推薦閱讀:

Struts中異步傳送XML和JSON類型的數據 http://www.linuxidc.com/Linux/2013-08/88247.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2學習筆記-Value Stack(值棧)和OGNL表達式 http://www.linuxidc.com/Linux/2015-07/120529.htm

struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved