攔截器是Struts2的核心所在。當須要擴展Struts2的功能時,只須要提供相應的攔截器,並將它配置在Struts容器中便可。反之,若是不須要某個功能,也只須要取消該攔截器便可。
java
定義攔截器使用<interceptor…/>元素。其格式爲:程序員
<interceptor name="攔截器名" class="攔截器實現類"></interceptor>
只要在<interceptor..>與</interceptor>之間配置<param…/>子元素便可傳入相應的參數。其格式以下:
apache
<interceptor name="myInterceptor" class="org.tool.MyInterceptor"> <param name="參數名">參數值</param> ... </interceptor>
一般狀況下,一個Action要配置不只一個攔截器,每每多個攔截器一塊兒使用來進行過濾。這時就會把須要配置的幾個攔截器組成一個攔截器棧。定義攔截器棧用<interceptor-stack name="攔截器棧名"/>元素,因爲攔截器棧是由各攔截器組合而成的,因此須要在該元素下面配置<interceptor-ref …/>子元素來對攔截器進行引用。其格式以下:jsp
<interceptor-stack name="攔截器棧名"> <interceptor-ref name="攔截器一"></interceptor-ref> <interceptor-ref name="攔截器二"></interceptor-ref> <interceptor-ref name="攔截器三"></interceptor-ref> </interceptor-stack>
注意:在配置攔截器棧時用到的攔截器必須是已經配置好的攔截器。攔截器棧也能夠引用攔截器棧。 spa
若是爲包指定了某個攔截器,則該攔截器會對每一個Action起做用,可是若是顯式地爲某個Action配置了攔截器,則默認的攔截器將不會起做用。默認攔截器用<default-interceptor-ref name=""/>元素來定義。每一個包只能指定一個默認的攔截器,若是須要指定多個攔截器共同做爲默認攔截器,則應該將這些攔截器定義成攔截器棧,而後把這個攔截器棧配置爲默認的攔截器便可。.net
下面是默認攔截器的配置方法:code
<package name="包名"> <interceptors> <interceptor name="攔截器一" class="攔截器實現類"></interceptor> <interceptor name="攔截器二" class="攔截器實現類"></interceptor> <interceptor-stack name="攔截器棧名"> <interceptor-ref name="攔截器一"></interceptor-ref> <interceptor-ref name="攔截器二"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="攔截器名或攔截器棧名"></default-interceptor-ref> </package>
Struts 2提供了一些接口或類供程序員自定義攔截器。如Struts 2提供了com.opensymphony.xwork2.interceptor.Interceptor接口,程序員只要實現該接口就可完成攔截器實現類。該接口的代碼以下:xml
import java.io.Serializable; import com.opensymphony.xwork2.ActionInvocation; public interface Interceptor extends Serializable{ void init(); String intercept(ActionInvocation invocation) throws Exception; void destroy(); }
該接口中有三個方法:對象
init():該方法在攔截器被實例化以後、攔截器執行以前調用。blog
intercept(ActionInvocation invocation):該方法用於實現攔截的動做。該參數調用其invoke方法,將控制權交給下一個攔截器,或者交給Action類的方法。
destroy():該方法與init()方法對應,攔截器實例被銷燬以前調用,用於銷燬在init()方法中打開的資源。
Struts 2提供了AbstractInterceptor類,該類提供了init()方法和destory()方法的空實現。在通常的攔截器實現中,都會繼承該類,由於通常實現的攔截器是不須要打開資源的,故無需實現這兩個方法,繼承該類會更簡潔。
下面來配置攔截器,若是輸入框中輸入的內容是「hello」,返回當前頁面。實現該功能只須要在原項目的基礎上配置攔截器便可。首先編寫攔截器實現類,代碼以下:
package org.tool; import org.action.StrutsAction; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor{ public String intercept(ActionInvocation arg0) throws Exception { // 獲得StrutsAction類對象 StrutsAction action=(StrutsAction)arg0.getAction(); // 若是Action類中的name屬性的值爲"hello",返回錯誤頁面 if(action.getName().equals("hello")){ return Action.ERROR; } // 繼續執行其餘攔截器或Action類中的方法 return arg0.invoke(); } }
在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="default" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="org.tool.MyInterceptor"></interceptor> </interceptors> <default-interceptor-ref name=""></default-interceptor-ref> <action name="struts" class="org.action.StrutsAction"> <result name="success">/welcome.jsp</result> <result name="error">/hello.jsp</result> <result name="input">/hello.jsp</result> <!--攔截配置在result後面 --> <!--使用系統默認攔截器棧 --> <interceptor-ref name="defaultStack"></interceptor-ref> <!--配置攔截器 --> <interceptor-ref name="myInterceptor"></interceptor-ref> </action> </package> </struts>
備註
改爲 "http://struts.apache.org/dtds/struts-2.1.dtd"就能夠了,緣由未知。
通過這樣簡單的配置後,從新部署項目,在運行界面輸入「hello」,也會通過攔截返回到當前頁面,如圖3.十一、圖3.12所示。
圖3.11 運行界面
圖3.12 提交後返回當前頁面