Struts2框架 之 拦截器
过滤器:自动登录
/* 所有请求
LoginServlet。退出
拦截器:相当于过滤器,插件式-针对方法
登录:管理操作之前,做权限判断
Struts2标签 jstl标签
创建拦截器
1.方式一:实现Interceptor接口
package a_interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
// 方式一:实现Interceptor接口
public class MyInterceptor1 implements Interceptor {
@Override
// 对象销毁时调用
public void destroy() {
}
@Override
// 对象创建初始化时调用
public void init() {
}
@Override
// 核心拦截方法
public String intercept(ActionInvocation invocation) throws Exception {
// Action前处理
System.out.println("MyInterceptor1");
// 放行 - 下一个拦截器
invocation.invoke();
// Action后处理
// 返回值
return null;
}
}
2.方式二:继承AbstractInterceptor类
package a_interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
// 方式二:继承AbstractInterceptor类
public class MyInterceptor2 extends AbstractInterceptor {
@Override
// 核心拦截方法
public String intercept(ActionInvocation invocation) throws Exception {
// Action前处理
System.out.println("MyInterceptor1");
// 放行 - 下一个拦截器
invocation.invoke();
// Action后处理
// 返回值
return null;
}
}