struts2拦截器的方法过滤功能在Action动态方法调用中的应用 .
2011-11-14 11:42
423 查看
拦截器是指动态拦截Action调用的对象,在执行Action中的方法之前,系统会首先执行struts.xml引用的拦截器,它能够在Action执行前后做一些相应的工作。但是,有些时候我们并不需要拦截器对Action中的某些方法进行拦截,于是就出现了拦截器的方法过滤功能,其具体实现是这样的:要实现此功能首先必须继承MethodFilterInterceptor类,然后重写此类中的doIntercept方法即可,此类中还有两个方法,分别是exclude和include,前者用来设置不需要拦截的方法,后者用来设置需要拦截的方法。
众所周知,平常我们在struts.xml中配置Action是往往是这样的:
<action name="action的名称" class="类名" method="Action中的方法">
</action>
如果需要使用拦截器的话,就在action标签内加入<interceptor-ref>调用需要的拦截器。但是这样的话,我们要拦截器的方法过滤功能有什么用呢,如果我们不想给此方法使用拦截器,直接不用<interceptor-ref>标签引入拦截器就可以了,为什么要煞费苦心的开发方法过滤功能呢?
答案是这样的:原来我们动态调用Action中的方法来处理业务逻辑有两种方式,一种是在<action>标签中指定method属性,一种是在<action>标签中不指定method属性,显然上面提到的是指定method属性;不指定method属性的方式如下所示:
<s:form action="Action的名称!方法名称">
然后再配置Action如下:
<action name="action的名称" class="类名">
</action>
在这种方式下,如果在视图页面中有多个按钮,每个按钮提交事件给同一个Action,但是对应Action中不同的方法,而在struts.xml文件中只配置一次Action。如果Action中的有些方法需要拦截,有些方法不需要拦截,这时拦截器的方法过滤功能就起到它的作用了,可以通过方法过滤功能来设置需要拦截和不需要拦截的方法。
说了不少了,还是给大家一个演示的例子吧,这个例子就是采用不指定method的方法调用,并且使用了方法过滤功能:
———————————————————————————————————————
视图:index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<s:form name="form1" action="public!execute1.action">
<s:submit value="测试1"></s:submit>
</s:form>
<s:form name="form2" action="public!execute2.action"><br>
</s:form>
<s:form name="form3" action="public!execute3.action">
<s:submit value="测试3"></s:submit>
</s:form>
</body>
</html>
Action:MethodFilterAction.java
package dr.action;
import com.opensymphony.xwork2.ActionSupport;
public class MethodFilterAction extends ActionSupport{
public String execute(){
System.out.println("excute方法执行!");
return SUCCESS;
}
public String execute1(){
System.out.println("excute1方法执行!");
return SUCCESS;
}
public String execute2(){
System.out.println("excute2方法执行!");
return SUCCESS;
}
public String execute3(){
System.out.println("excute3方法执行!");
return SUCCESS;
}
}
拦截器:MethodInterceptor.java
package dr.interceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.opensymphony.xwork2.ActionInvocation;
public class MethodInterceptor extends MethodFilterInterceptor{
public String doIntercept(ActionInvocation ai)throws Exception{
System.out.println("拦截器起作用了!");
return ai.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="struts2" extends="struts-default">
<interceptors>
<interceptor name="methodInterceptor" class="dr.interceptor.MethodInterceptor"></interceptor>
</interceptors>
<action name="public" class="dr.action.MethodFilterAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="methodInterceptor">
<param name="excludeMethods">execute,execute2,execute3</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
众所周知,平常我们在struts.xml中配置Action是往往是这样的:
<action name="action的名称" class="类名" method="Action中的方法">
</action>
如果需要使用拦截器的话,就在action标签内加入<interceptor-ref>调用需要的拦截器。但是这样的话,我们要拦截器的方法过滤功能有什么用呢,如果我们不想给此方法使用拦截器,直接不用<interceptor-ref>标签引入拦截器就可以了,为什么要煞费苦心的开发方法过滤功能呢?
答案是这样的:原来我们动态调用Action中的方法来处理业务逻辑有两种方式,一种是在<action>标签中指定method属性,一种是在<action>标签中不指定method属性,显然上面提到的是指定method属性;不指定method属性的方式如下所示:
<s:form action="Action的名称!方法名称">
然后再配置Action如下:
<action name="action的名称" class="类名">
</action>
在这种方式下,如果在视图页面中有多个按钮,每个按钮提交事件给同一个Action,但是对应Action中不同的方法,而在struts.xml文件中只配置一次Action。如果Action中的有些方法需要拦截,有些方法不需要拦截,这时拦截器的方法过滤功能就起到它的作用了,可以通过方法过滤功能来设置需要拦截和不需要拦截的方法。
说了不少了,还是给大家一个演示的例子吧,这个例子就是采用不指定method的方法调用,并且使用了方法过滤功能:
———————————————————————————————————————
视图:index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<s:form name="form1" action="public!execute1.action">
<s:submit value="测试1"></s:submit>
</s:form>
<s:form name="form2" action="public!execute2.action"><br>
</s:form>
<s:form name="form3" action="public!execute3.action">
<s:submit value="测试3"></s:submit>
</s:form>
</body>
</html>
Action:MethodFilterAction.java
package dr.action;
import com.opensymphony.xwork2.ActionSupport;
public class MethodFilterAction extends ActionSupport{
public String execute(){
System.out.println("excute方法执行!");
return SUCCESS;
}
public String execute1(){
System.out.println("excute1方法执行!");
return SUCCESS;
}
public String execute2(){
System.out.println("excute2方法执行!");
return SUCCESS;
}
public String execute3(){
System.out.println("excute3方法执行!");
return SUCCESS;
}
}
拦截器:MethodInterceptor.java
package dr.interceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.opensymphony.xwork2.ActionInvocation;
public class MethodInterceptor extends MethodFilterInterceptor{
public String doIntercept(ActionInvocation ai)throws Exception{
System.out.println("拦截器起作用了!");
return ai.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="struts2" extends="struts-default">
<interceptors>
<interceptor name="methodInterceptor" class="dr.interceptor.MethodInterceptor"></interceptor>
</interceptors>
<action name="public" class="dr.action.MethodFilterAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="methodInterceptor">
<param name="excludeMethods">execute,execute2,execute3</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
相关文章推荐
- Struts2 Action中动态方法调用、通配符的使用
- Struts2的动态调用Action方法和默认Action
- Struts2 拦截器常用的获取Action、调用的方法名字、Session等
- struts2 动态调用action中方法
- 每当使用struts2动态方法调用时,总是出现错误:There is no Action mapped for namespace [/user] and action name [user!add]
- Struts2中Action的动态调用方法
- struts2动态方法调用 ActionName!methodName.action
- 第三章Struts2 Action中动态方法调用、通配符的使用
- Struts2 Action中动态方法调用、通配符的使用
- struts2学习笔记--------动态方法调用和使用通配符定义action
- 使用<a>标签调用struts2的action、传递动态参数、以及参数中出现加号等特殊字符的处理方法
- struts2配置及ActionMethod_DMI_动态方法调用
- 关于Struts2 使用! 感叹号 动态调用Action中的方法
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- struts2 Action专题之二:动态方法的调用
- Struts2初学者---动态方法调用action!方法名报错
- Struts2-动态调用action中的方法
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- Struts2-动态方法调用-Could not find action or result-Method update for action user is not allowed
- struts2_7_Action类中方法的动态调用