继承AbstractInterceptor拦截器
定义拦截器时,可以直接继承AbstractInterceptor抽象类(该类实现了Interceptor接口,并对init和destory方法进行了空实现),再实现抽象方法即可。
实例分析:
1、定义继承AbstractInterceptor的拦截器
<span style="font-size:18px;">public class TheInterceptor2 extends AbstractInterceptor
{
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
System.out.println("interceptor2 before");
String result = invocation.invoke();
System.out.println("Interceptor2:"+invocation.getAction().getClass());
System.out.println("interceptor2 after");
return result;
}
}</span>
2、配置struts.xml
<interceptors>
<interceptor name="interceptor1" class="cn.sict.interceptor.TheInterceptor1"></interceptor>
<interceptor name="interceptor2" class="cn.sict.interceptor.TheInterceptor2"></interceptor>
</interceptors><pre name="code" class="html"><action name="login1" class="cn.sict.struts2.LoginAction">
<result name="success">/result.jsp</result>
<interceptor-ref name="interceptor1"></interceptor-ref>
<interceptor-ref name="interceptor2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
3、运行结果如下:
结果分析:对LoginAction拦截,如果存在下一个拦截器,会执行下一个拦截器,没有的话,则返回执行Action。
方法拦截器
方法拦截器对指定方法进行拦截的拦截器。
MethodFilterInterceptor继承自AbstractInterceptor,对指定方法进行拦截,相关的参数时excludeMethods、includeMethods。如果方法过滤拦截器,没有指定includeMethods参数,也没有指定excludeMethods参数,那所有的方法都要被拦截。如果指定了includeMethods方法,那只拦截includeMethods方法。
源码分析:该方法拦截器中有intercept方法和dointercept()方法,而dointercept方法是抽象的,需要再子类中进行重写。那intercept和dointercept方法有什么关系呢?从源码可以分析出,如果有方法需要被拦截,就会调用doInteceptor,否则,就直接调用action方法,不拦截,正常执行程序。applyInterceptor就是验证拦截器是否能够应用在includeMethod上和excludeMethods上。
</pre><br /><pre name="code" class="java">@Override
public String intercept(ActionInvocation invocation) throws Exception {
if (applyInterceptor(invocation)) {
return doIntercept(invocation);
}
return invocation.invoke();
}
protected boolean applyInterceptor(ActionInvocation invocation) {
String method = invocation.getProxy().getMethod();
// ValidationInterceptor
boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
if (log.isDebugEnabled()) {
if (!applyMethod) {
log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");
}
}
return applyMethod;
}
/**
* Subclasses must override to implement the interceptor logic.
*
* @param invocation the action invocation
* @return the result of invocation
* @throws Exception
*/
protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
}
通过param配置includeMethods和excludeMethods参数。
<interceptor-ref name="interceptor2">
<!-- 要去拦截的方法 ,表示对execute方法进行拦截-->
<param name="includeMethods">execute,myExecute</param>
</interceptor-ref>
Struts定义拦截器的三种方法:
1)实现Intercept接口,重写intercept方法
2)继承AbstractInterceptor类,重写intercept方法
3)继承MethodFilterInterceptor类,对特定的方法进行过滤拦截
利用拦截器检查session中的内容,实例:
检查是否登陆的拦截器,在项目开发中,除了登陆页不需要验证是否登陆,其他页面都需要验证是否登陆,没有登陆的话,就转到登陆页进行登陆。
1)LoginAction
public class LoginAction extends ActionSupport implements Preparable{
private String username;
private String password;
HttpServletRequest request;
private LoginService loginService=new LoginService();
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception
{
if(this.loginService.isLogin(username,password))
{
User user=new User();
user.setUsername(username);
user.setPassword(password);
ActionContext.getContext().getSession().put("userInfo", user);
return SUCCESS;
}
return INPUT;
}
2)编写LoginInterceptor
public class LoginInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
if(LoginAction.class==invocation.getAction().getClass())//如果是登陆页面,不需要拦截
{
return invocation.invoke();
}
Map map=invocation.getInvocationContext().getSession();
if(null==map.get("userInfo"))//如果session中不存在userInfo,需要返回登陆
{
return Action.LOGIN;
}
return invocation.invoke();
}
}
3)配置struts.xml文件
<?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>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="LoginInterceptor" class="cn.sict.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myDefaultInterceptorStack">
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefaultInterceptorStack">
</default-interceptor-ref>
<global-results>
<result name="login">/error.jsp</result>//如果结果为login,就提示没有登陆的错误信息
</global-results>
<action name="login1" class="cn.sict.struts2.LoginAction">
<result name="success">/result.jsp</result>
<action>....</action>
</package>
</struts>