首页 > 学院 > 开发设计 > 正文

Struts2拦截器

2019-11-06 06:41:56
字体:
来源:转载
供稿:网友

1、java类中创建一个interceptor

首先在创建一个class类,继承AbstractInterceptor的抽象类,实现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文件中进行配置

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是默认的拦截器名称,不可更改

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、在自定义拦截器后,默认的拦截器将会失效;

2、拦截器采用的是就近原则,先从局部查找,然后才是全局。

3、 指定拦截器

指定拦截器,和普通的拦截器大同小异

 

首先,创建一个class类,继承MethodFilterInterceptor抽象类,实现其方法

[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、重复表单提交

a) 、跳转的时候,不再用转发的方式,用重定向;

b) 、使用token拦截器

1、在JSP页面中,使用Struts2的标签库

[html] view plain copy print?<%@ taglib uri=“/struts-tags” prefix=“s”%>  
<%@ taglib uri="/struts-tags" prefix="s"%>

然后在form表单中使用<s:token>标签

[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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表