网站开发的时候进行资源访问控制是必须的,经常使用的方式是在web.xml中添加过滤器进行资源过滤,在struts2中我们可以使用拦截器对资源进行控制
public class LoginInterceptor extends AbstractInterceptor
{
/*
* 进行登录拦截 看是否已经登入 (non-Javadoc) 相当于一个过滤器
*
* @see
* com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com
* .opensymphony.xwork2.ActionInvocation)
*/
@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
Map map = invocation.getInvocationContext().getSession();
//。。。其他逻辑
if (LoginImpl.class == invocation.getAction().getClass())
{
// 让登入的界面通过
return invocation.invoke();
}
if (map.get("login") == null)
{
// 返回登录界面
return Action.LOGIN;
}
return invocation.invoke();
}
}
原文:http://blog.csdn.net/cml_blog/article/details/22982395