IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!
Struts2的核心功能是通过一个个interceptor来实现的,那么用户可以自定义一个interceptor来实现权限控制功能。
步骤一:自定义一个类,该类实现Interceptor接口,重写其intercept()方法。如:
package edu.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class PermissionInterceptor implements Interceptor {
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object user = ActionContext.getContext().getSession().get("user");
if(user!=null)
return invocation.invoke();//有权限,调用方法
ActionContext.getContext().put("message", "你还没有登陆,请先登录!");
return "message";//无权限,返回视图,其返回值也会匹配struts.xml总的result视图,然后返回页面。
}
}