首先在創(chuàng)建一個class類,繼承AbstractInterceptor的抽象類,實(shí)現(xiàn)intercept方法
[java] view plain copy PRint?public class MyInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { return null; } }
public class MyInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { return null; }}2、在Struts.xml文件中進(jìn)行配置
A)、定義攔截器
[html] view plain copy print?<interceptors> <interceptor name=“攔截器名” class=“攔截器的包名”></interceptor> <interceptor-stack name=“攔截器棧名”> <interceptor-ref name=“defaultStack”></interceptor-ref> <interceptor-ref name=“攔截器名”></interceptor-ref> </interceptor-stack> </interceptors> 
<interceptors> <interceptor name="攔截器名" class="攔截器的包名"></interceptor> <interceptor-stack name="攔截器棧名"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="攔截器名"></interceptor-ref> </interceptor-stack></interceptors>Ps:defaultStack是默認(rèn)的攔截器名稱,不可更改
B)、使用攔截器
[html] view plain copy print?<default-interceptor-ref name=“攔截器棧名”></default-interceptor-ref> 
<default-interceptor-ref name="攔截器棧名"></default-interceptor-ref>除此之外,還有一種自定義攔截器棧的方式
A)、定義攔截器
[html] view plain copy print?<interceptors> <interceptor name=“攔截器名” class=“攔截器的包名”></interceptor> </interceptors> 
<interceptors> <interceptor name="攔截器名" class="攔截器的包名"></interceptor></interceptors>B)、使用攔截器
[html] view plain copy print?<action name=“test” class=“com.zuxia.action.TestAction”> <interceptor-ref name=“defaultStack”></interceptor-ref> <interceptor-ref name=“攔截器名”></interceptor-ref> <result>/success.jsp</result> </action> 
<action name="test" class="com.zuxia.action.TestAction"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="攔截器名"></interceptor-ref> <result>/success.jsp</result></action>注意:
1、在自定義攔截器后,默認(rèn)的攔截器將會失效;
2、攔截器采用的是就近原則,先從局部查找,然后才是全局。
3、 指定攔截器
指定攔截器,和普通的攔截器大同小異
首先,創(chuàng)建一個class類,繼承MethodFilterInterceptor抽象類,實(shí)現(xiàn)其方法
[html] view plain copy print?public class MeInterceptor extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation arg0) throws Exception { System.out.println(“這是指定攔截”); arg0.invoke(); return null; } } 
public class MeInterceptor extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation arg0) throws Exception { System.out.println("這是指定攔截"); arg0.invoke(); return null; }}然后,在struts.xml文件中配置,配置起來也和原來的差不多
[html] view plain copy print?<interceptor name=“MeInterceptor” class=“com.test.intercept.MeInterceptor”> <param name=“includeMethods”>add,update</param> //<span style=“font-family: 宋體;”>這是用指定那些攔截器可用</span> <param name=“excludeMethods”>add,update</param>//<span style=“font-family: 宋體;”>這是指那些攔截器不可用</span> </interceptor> 
<interceptor name="MeInterceptor" class="com.test.intercept.MeInterceptor"> <param name="includeMethods">add,update</param> //<span style="font-family: 宋體;">這是用指定那些攔截器可用</span> <param name="excludeMethods">add,update</param>//<span style="font-family: 宋體;">這是指那些攔截器不可用</span></interceptor>4、重復(fù)表單提交
a) 、跳轉(zhuǎn)的時候,不再用轉(zhuǎn)發(fā)的方式,用重定向;
b) 、使用token攔截器
1、在JSP頁面中,使用Struts2的標(biāo)簽庫
[html] view plain copy print?<%@ taglib uri=“/struts-tags” prefix=“s”%> 
<%@ taglib uri="/struts-tags" prefix="s"%>然后在form表單中使用<s:token>標(biāo)簽
[html] view plain copy print?<form action=“/Struts2_test/tokntest”> <s:token></s:token> 測試:<input type=“text” name=“test”> <input type=“submit” value=“提交”> </form> 
<form action="/Struts2_test/tokntest"> <s:token></s:token> 測試:<input type="text" name="test"> <input type="submit" value="提交"></form>2、在struts.xml文件中配置
[html] view plain copy print?<action name=“tokntest” class=“com.test.action.TokeAction”> <result name=“invalid.token”>/error.jsp</result> <result>/success.jsp</result> </action> 
<action name="tokntest" class="com.test.action.TokeAction"> <result name="invalid.token">/error.jsp</result> <result>/success.jsp</result></action>
新聞熱點(diǎn)
疑難解答