* 2.1实现拦截器类 *
public class Interceptor extends AbstractInterceptor{
//简单的拦截器名字
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
//取得被拦截的action实例
LoginAction action = (LoginAction) invocation.getAction();
//打印执行开始的实现
System.out.println(name+"拦截器的动作-------"+
"开始登陆action的时间为:"+new Date()
);
//取得开始执行的时间
long startTime = System.currentTimeMillis();
//如果该拦截器之后没有其他的拦截器,直线action的excute方法
//注意这句话###############################
//#######################################
*String result = invocation.invoke();*
--------------------------------------
//打印执行结束后的时间
System.out.println(name+"执行结束的时间------"+
"执行完登陆的时间"+new Date()
);
long endTime = System.currentTimeMillis();
System.out.println("总共用时"+ (endTime - startTime));
return result;
}
}
上面的拦截器仅仅在被拦截的方法之前,打印出执行action的时间,并记录开始执行action的时刻,执行action的execute方法之后再次打印出时间,String result = invocation.invoke();这句代码将会调用目标action 的excute 方法
在上一篇中我们说拦截器一般有三个方法:
1:init();
2:destroy();
3:intercept();
在这个拦截器中,我们并没有打开相应的资源,所以,不需要事先init()和destroy();
在实现intercept(ActionInvocation invocation)方法时,可以获得ActionInvocation参数,这个参数又可以获得被拦截的action实例,有了这个实例,几乎可以获得action的所有控制权:可以解析HTTP请求中的参数,也可以将HTTP请求中的HttpServletRequest & HttpServletResponse中的实例传递给action中
2.2 :使用拦截器:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 通过常量配置Struts2中的国际化资源-->
<constant name="struts.custom.i18n.resources" value="globalMessage"/>
<!-- 配置解码集 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<!-- 配置本系统中使用的包 -->
<package name="liang" extends="struts-default">
<interceptors>
<!-- 配置拦截器 -->
<interceptor name="myInterceptor" class="liang.Interceptor"/>
</interceptors>
<!-- 配置自己的action -->
<action name="login" class="liang.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/login.jsp</result>
<!-- 拦截器一般配置在result之后 -->
<!-- 配置系统默认的拦截器,如果配置了自定义的拦截器,默认的拦截器将会失去作用
必须显示的指定系统自定义的拦截器 -->
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="myInterceptor"/>
</action>
</package>
</struts>
2.3:拦截方法的拦截器:
在有些时候,我们并不想拦截所有的action 此时便需要使用struts2中的拦截器方法的过滤特性
为了实现方法的过滤性,Struts2提供了一个MethodFilterInterceptor类,该类是AbstractInterceptor类的
子类,如果用户想实现的拦截器支持方法的过滤特性,需要继承MethodFilterInterceptor
下面自定义一个FilterInterceptor带过滤方法功能的拦截器:
package pageInterCeptor;
import java.util.Date;
import Action.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//过滤方法的过滤器,应该继承MethodFilterInterceptor
public class MyFilterInterceptor extends MethodFilterInterceptor {
//简单的拦截器的名称
private String name;
//为该过滤器设置名字
public void setName(String name) {
this.name = name;
}
//重写doIntercept,对action进行拦截逻辑
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
//取得被拦截的action的实例
LoginAction login = (LoginAction) invocation.getAction();
//打印执行action的时间
System.out.println(name+"拦截器的动作---开始的时间\t"+new Date());
//指定后一个action 或者直接执行action的excute方法
String result = invocation.invoke();
//打印执行结束的时间
System.out.println(name+"拦截器的动作--拦截器结束的时间\t"+new Date());
return result;
}
}
跟前面的拦截器相比似乎没有太大的改变,只有两个地方需要注意
1:继承的是MethodFilterInterceptor拦截器
2:重写的是doInterceptor拦截器
在MethodFilterInterceptor方法中,额外的增加了两个方法:
public void setExcludeMethods(String execludeMethods):排除需要过滤的方法,所有在execludeMethods中出现的方法都不会被拦截
public void setIncludeMethods(String includeMethods):设置的都会被拦截
如果一个方法同时出现在execludeMethods 以及includeMethods 则该方法会被拦截
方法过滤实例如下所示:
<package name="liang" extends="struts-default">
<interceptors>
<!-- 配置方法的拦截器 -->
<interceptor name="myFilter" class="pageInterCeptor.MyFilterInterceptor">
<param name="name">方法过滤器</param>
</interceptor>
</interceptors>
<!-- 指定处理用户的action -->
<action name="login" class="liang.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="myFilter">
<param name="name">改名后的拦截器</param>
<!-- 指定execute方法不需要拦截 如果需要拦截多个方法,方法之间使用逗号隔开-->
<param name="execludeMethods">execute</param>
</interceptor-ref>
</action>
</package>
上面的拦截器不会拦截execute方法,如果一个action中只有execute方法,则不会被拦截,控制台不会打印任何的输出
execute则表示只拦截execute方法