您的位置:首页 > 编程语言 > Java开发

四:Struts拦截器简单介绍

2016-11-13 11:30 323 查看
一:简单拦截器

拦截器在概念上与servlet过滤器或JDK代理类相同。拦截器允许横切功能,把action以及框架分开实现。你可以使用拦截器实现以下操作:

在调用action之前提供预处理逻辑。

在调用action后提供后处理逻辑。

捕获异常,以便可以执行备用处理。

Struts2框架中提供的许多功能都是使用拦截器实现的,包括异常处理,文件上传,生命周期回调和验证等。事实上,由于Struts2将其大部分功能基于拦截器,因此不太可能为每个action分配7个或8个拦截器。

定义拦截器:

public class MyInterceptor extends AbstractInterceptor{
public String intercept(ActionInvocation arg0) throws Exception{
System.out.println("action调用前");
String result=arg0.invoke();//调用下一个拦截器
System.out.println("action调用后");
return result;
}
}


*上面是一个简单的拦截器定义,首先继承AbstractInterceptor类,然后重写里面的intercept方法。invoke()方法意思是调用执行action。

配置拦截器:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<interceptors>
<!-- 		声明拦截器 -->
<interceptor name="myInterceptor" class="org.interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myDefaultInterceptor">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>

<action name="myLogin" class="org.action.LoginAction" method="execute">
<!-- 		在action中引用拦截器 -->
<interceptor-ref name="myInterceptor"></interceptor-ref>
</action>
</package>
</struts>


*上面是对前面定义的拦截器的配置,<interceptor-stack name="myDefaultInterceptor"></interceptor-stack>为拦截器栈,可以容纳多个拦截器,执行顺序为从上到下。

*当为一个action配置拦截器时,默认的拦截器就不起作用了,这时需要显式的配置"defaultStack"这个拦截器栈。

*配置默认拦截器的方法:<default-interceptor-ref name=""></default-interceptor-ref>


action:

public class LoginAction extends ActionSupport{

public String execute() throws Exception{
System.out.println("action执行中");
return NONE;
}
}

***执行上面代码的结果为:
action调用前

action执行中

action调用后

***

原因:在执行action中的 execute() 方法之前,拦截器拦截,执行下面这段代码:

        System.out.println("action调用前");

        String result=arg0.invoke();//调用action

运行action中的 execute(),运行完之后回头再执行下面这段代码:

        System.out.println("action调用后");

        return result;

拦截器通过invoke()方法,在调用action之前提供预处理逻辑,在调用action后提供后处理逻辑。

二:方法过滤拦截器

**该拦截器可以过滤方法,指定哪些方法要被拦截,哪些不要被拦截。

定义拦截器

public class MyInterceptor extends MethodFilterInterceptor{
public String doIntercept(ActionInvocation arg0) throws Exception{
System.out.println("action调用前");
String result=arg0.invoke();
System.out.println("action调用后");
return result;
}
}
*继承MethodFilterInterceptor类,重写doIntercept() 方法

配置拦截器

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<!-- 		声明拦截器 -->
<interceptors>
<interceptor name="myInterceptor" class="org.interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myDefaultInterceptor">
<interceptor-ref name="myInterceptor">
<param name="includeMethods">execute</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>

<action name="myLogin" class="org.action.LoginAction" method="execute">
<!-- 		引用拦截器 -->
<interceptor-ref name="myInterceptor"></interceptor-ref>
</action>
</package>
</struts>


<param name="includeMethods">method1,method2</param>指定拦截哪些方法

<param name="excludeMethods">method1,method2</param>指定不拦截哪些方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息