struts2 攔截器
阿新 • • 發佈:2017-05-23
struts2 攔截器
攔截器,作用是在執行方法執行,進行過濾攔截。
struts2 攔截器的幾種方式
聲明攔截器,然後在Action中引用聲明的攔截器
默認攔截器,在當前package下有且只能有一個默認攔截器
作用:在當前package下所有的action都會經過默認攔截器攔截,當action中引入攔截器時,
默認攔截器失效。
攔截器棧,把相關一組攔截器放在棧中,這樣在action中就不必引入多個攔截器,直接引入攔截器棧就可以了,系統默認攔截器棧是 defaultStack ,如果自定義了攔截器棧,系統默認攔截器棧就不會起作用,這樣我們需要手動引入系統默認攔截器棧,defaultStack
註意:對於攔截器先後順序,引入的前後就是攔截器的先後順序。
自定義結果類型
對於默認的結果類型是 dispatch redirect redirectAction chain
下面看下具體的實現步驟
介紹攔截器
package com.struts2.action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; @SuppressWarnings("serial") public class MyInter implements Interceptor{ /* * 在攔截器結束後,的銷毀方法 * */ @Override public void destroy() { } /** * 已進入攔截器,初始化方法 */ @Override public void init() { } /** * 這裏是攔截器的核心方法,寫一些在執行action方法之前的一些邏輯 */ @Override public String intercept(ActionInvocation ai) throws Exception { System.out.println("-----攔截器-------"); ai.invoke(); return null; } }
2.攔截器寫好了,我們看下配置,引入了自己的攔截器,系統默認就不會引用,需要添加上
<interceptors> <interceptor name="myInter" class="com.struts2.action.MyInter"/> </interceptors>
攔截器聲明好了之後,如果想在某個action中用到攔截器,需要引用配置,配置信息如下
<action name="*_*" class="com.struts2.action.{1}" method="{2}"> <!-- 引入攔截器 --> <interceptor-ref name="myInter"></interceptor-ref> <!-- 系統默認攔截器棧 --> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="login">/login.jsp</result> </action>
3.默認攔截器配置
<!-- 默認攔截器,當前package下的所有action都會執行這個攔截器,如果在action中引用了攔截器, 默認的就會失效 默認攔截器,可以是一個攔截器,也可以是一個攔截器棧 如果是一個攔截器,那麽在action方法不會有系統默認攔截器棧,如果在action中引入系統默認攔截器, 那麽默認攔截器又不會起作用,所有通常來說,默認攔截器寫成攔截器棧,在默認攔截器棧中引入系統 默認攔截器 --> <!-- 聲明攔截器 --> <interceptors> <interceptor name="myInter" class="com.struts2.action.MyInter"/> <interceptor name="myInter1" class="com.struts2.action.MyInter1"/> <interceptor name="myInter2" class="com.struts2.action.MyInter2"/> <!-- 攔截器棧,把多個攔截器大包,這樣引用的時候,就可以直接引用攔截器棧名稱 --> <interceptor-stack name="mi"> <interceptor-ref name="myInter"/> <interceptor-ref name="myInter1"/> <interceptor-ref name="myInter2"/> <!-- 把系統默認攔截器棧引入進來 --> <interceptor-ref name="defaultStack"/></interceptor-stack> </interceptors> <!-- 默認攔截器,當前package下的所有action都會有這個攔截器,除非在action中引用攔截器,默認的就會失效 --> <default-interceptor-ref name="mi"></default-interceptor-ref>
4.攔截器棧
<!-- 聲明攔截器 --> <interceptors> <interceptor name="myInter" class="com.struts2.action.MyInter"/> <interceptor name="myInter1" class="com.struts2.action.MyInter1"/> <interceptor name="myInter2" class="com.struts2.action.MyInter2"/> <!-- 攔截器棧,把多個攔截器大包,這樣引用的時候,就可以直接引用攔截器棧名稱 --> <interceptor-stack name="mi"> <interceptor-ref name="myInter"/> <interceptor-ref name="myInter1"/> <interceptor-ref name="myInter2"/> </interceptor-stack> </interceptors> <action name="*_*" class="com.struts2.action.{1}" method="{2}"> <!-- 引入攔截器棧 --> <interceptor-ref name="mi"></interceptor-ref> <!-- 引入系統默認攔截器棧,如果沒有系統默認攔截器棧, 那麽關於系統的方法就不能使用,比如,從頁面傳參,action接收不到 --> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="login" type="redirect">/login.jsp</result> <result name="index" type="redirect">/index.jsp</result> <result name="error">/error.jsp</result> </action>
5.自定義返回結果類型
我們先實現自定義結果返回的跳轉方式
package com.struts2.action; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.result.StrutsResultSupport; import com.opensymphony.xwork2.ActionInvocation; /** * @author admin * 自定義攔截器結果類型 */ @SuppressWarnings("serial") public class CustomType extends StrutsResultSupport{ /** * 這裏我們默認跳轉方式為 重定向 */ @Override protected void doExecute(String location, ActionInvocation invocation) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); String contexPath = ServletActionContext.getRequest().getContextPath(); response.sendRedirect(contexPath+location); } }
看下如何配置
<!-- 自定義攔截器結果類型 --> <result-types> <result-type name="defa" class="com.struts2.action.CustomType"></result-type> </result-types> <action name="*_*" class="com.struts2.action.{1}" method="{2}"> <!-- 這裏type就是我們上面自定義的結果返回類型 --> <result name="login" type="defa">/login.jsp</result> </action>
匆匆的寫的,可能會有所疏漏,如有不足,請多多指導。
本文出自 “11134439” 博客,請務必保留此出處http://11144439.blog.51cto.com/11134439/1928509
struts2 攔截器