struts2学习(三)--拦截器
1.拦截器:拦截器是struts2的核心,struts2的众多功能都是通过拦截器来实现的。
2.拦截器的配置:
1)编写实现Interceptor接口的类。
void
destroy()
void
init()
intercept
, giving the Interceptor a chance to initialize any needed resources.String
intercept(ActionInvocation invocation)
ActionInvocation
or to short-circuit the processing and just return a String return code.实现该接口的类为单例,在服务器启动时创建,并会调用init()方法。
2)在struts.xml文件中定义拦截器。
注:自定义拦截器后,默认的拦截器就会失效。必须手动添加。并且放在拦截器的最后。
<interceptor-ref name="defaultStack"></interceptor>
3)在action中使用拦截器
注:拦截器配置在action中只能拦截action,而过滤器可以拦截所有的。
3.在实现Interceptor接口时,无论是否会使用到destroy和init方法,都要有一个空实现。为此struts2中提供 AbstractInterceptor抽象类,该类实现了Interceptor,并对init和destroy方法进行了空实现。
4.拦截器的执行顺序。见如下实例:
项目结构如下:
login.jsp代码如下:
<form action="loginAction.action" method="post"><input type="submit" value="提交"></form>
web.xml代码如下:
<filter> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
struts.xml中代码如下:
<struts><package name="struts2" extends="struts-default"><interceptors><interceptor name="interceptorTest01" class="com.interceptor.inteceptors.InterceptorTest01"></interceptor></interceptors><action name="loginAction" class="com.interceptor.action.LoginAction"><result name="success">/success.jsp</result><interceptor-ref name="interceptorTest01"><param name="name">test01</param></interceptor-ref><interceptor-ref name="interceptorTest01"><param name="name">test02</param></interceptor-ref><interceptor-ref name="interceptorTest01"><param name="name">test03</param></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></action></package></struts>
InterceptorTest01.java中代码如下:
package com.interceptor.inteceptors;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class InterceptorTest01 implements Interceptor{private String name;@Overridepublic void destroy() {}@Overridepublic void init() {System.out.println("InterceptorTest01 init method invoked...");}@Overridepublic String intercept(ActionInvocation arg0) throws Exception {System.out.println("intercept method start...");System.out.println(name);String result = arg0.invoke();System.out.println("intercept method end...");return result;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
LoginAction.java中代码如下:
public class LoginAction extends ActionSupport{@Overridepublic String execute() throws Exception {System.out.println("loginAction invoked...");return SUCCESS;}}
启动服务器时,会打印:
在页面点击提交按钮,会打印如下:
如果是在拦截方法之前,则配置在前面的拦截器,会先对用户的请求起作用。如果是在拦截方法之后,则配置在后面的拦截器,会先对用户的请求起作用。
5.方法拦截器:对指定的方法进行拦截。需要继承MethodFilterInterceptor抽象类,该类继承AbstractInterceptor抽象类。拦截是指拦截器中的方法是否执行。
struts中配置如下:
<interceptor-ref name="theinterceptor">
<param name="includeMethods">要拦截的action中的方法名</param>
<param name="excludeMethods">不需要拦截的方法名,多个方法间用逗号分开</param>
</interceptor-ref>