struts2的魅力之处在于强大的拦截器功能 当struts2的拦截器不满足需要时我们需要根据项目需求自定义拦截器
自定义拦截器需要实现Interceptor接口或者继续AbstractInterceptor这个抽象类 重写intercept()方法 AbstractInterceptor这个抽象类提供了destroy()和init()的空白方法
实现Interceptor 接口则需要对destroy()和init() 以及intercept()的方法
一般情况下我们继承AbstractInterceptor这个抽象类即可
<span style="white-space:pre"> </span>/**
* 实现登陆拦截
*/
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Map<String, Object> session=invocation.getInvocationContext().getSession();// 获取session
String username=(String)session.get("username");
//如果是空的话则返回到登陆页面 如果登陆的话则执行以下的拦截器
if(username==null||username.trim().equals("")){
return Action.LOGIN;
}
return invocation.invoke();
}