struts2的拦截器

来源:互联网 发布:js如何清空div的内容 编辑:程序博客网 时间:2024/05/13 21:58

本文主要介绍struts2的拦截器

一.配置框架提供的拦截器

拦截器与过滤器的概念不太一样,拦截器是指strut2中的概念,只能对action拦截,不能拦截别的,而过滤器指的web层的概念.

现在介绍一下timer拦截器和token拦截器的使用,timer拦截器是计算action的执行时间,token拦截器是防止表单重复提交.

下面是struts.xml的配置:

<action name="testTimerInterceptorAction" class="com.TestTimerInterceptorAction">        <interceptor-ref name="timer"></interceptor-ref>        <interceptor-ref name="token"></interceptor-ref>        <result>/ok.jsp</result>        <result name="invalid.token">/tokeneoor.jsp</result></action>
若要在当前包中的每个action中都配相同的拦截器,则只需在当前包下配置拦截器即可。
下面是提交表单的jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>    <%@taglib prefix="s"  uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><s:form action="testTimerInterceptorAction"><s:textfield name="name"></s:textfield><s:token></s:token><s:submit>注册</s:submit></s:form></body></html>
其中<s:token></s:token>其实是一个隐藏域向后台传递一个值,后台根据此值来判断表单是不是重复提交。

下面是TestTimerInterceptorAction.java测试类

package com;import com.opensymphony.xwork2.ActionSupport;public class TestTimerInterceptorAction extends ActionSupport {public String execute() throws InterruptedException{Thread.sleep(1000);return null;}}
此案可以测试TestTimerInterceptorAction的执行时间和表单是否进行了重复提交.

二.自定义拦截器

自定义拦截器,采用上文的struts.xml配置文件,在要拦截的action对应的包中添加自己配置的拦截器

如下:

 <interceptors>    <interceptor name="myInterceptor" class="com.MyInterceptor">    </interceptor>    </interceptors>

然后在要拦截的action中配置上面定义的拦截器,如下:

<interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="myInterceptor"></interceptor-ref>

注意:要加上框架默认的拦截器栈.

对应的拦截器要实现Interceptor接口

MyInterceptor.java文件如下:

package com;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class MyInterceptor implements Interceptor{@Overridepublic void destroy() {}@Overridepublic void init() {}@Overridepublic String intercept(ActionInvocation ai) throws Exception {TestTimerInterceptorAction  testTimerInterceptorAction= (TestTimerInterceptorAction) ai.getAction();testTimerInterceptorAction.setId("200");ai.invoke();return null;}}

不过此时timer和token拦截器和自定义的拦截器有冲突,原因暂时不明,去掉timer和token拦截器即可验证成功

三.方法拦截器(MethodInterceptor)

struts2可以对action中的方法进行拦截,配置拦截器如下:

<interceptor name="myMethodInterceptor" class="com.MyMethodInterceptor">    <param name="includeMethods">test1,test2</param>    <param name="excludeMethods">test3</param></interceptor>
其对应的方法拦截器要继承MethodFilterInterceptor

package com;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;public class MyMethodInterceptor extends MethodFilterInterceptor{@Overrideprotected String doIntercept(ActionInvocation ai) throws Exception {System.out.println("MyMethodInterceptor");ai.invoke();return null;}}
然后在要进行拦截的action中进行拦截器的引用,即可对某些方法进行拦截了。




0 0
原创粉丝点击