1.首先新建一个拦截器
//设置权限管理
public class PrivilegetInterceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
//验证是否存在后台信息
AdminUser existAdminUser=(AdminUser) ServletActionContext.getRequest().getSession().getAttribute("existAdminUser");
if(existAdminUser!=null){
//已经登录
return actionInvocation.invoke();
}else{
//没有登录
ActionSupport actionSupport=(ActionSupport) actionInvocation.getAction();
actionSupport.addActionError("用户没有登录,没有权限访问!");
return "loginFail";
}
}
}
2.在struts.xml的配置文件中,配置拦截器
<interceptors>
<interceptor name="PrivilegetInterceptor" class="com.zhlk.shop.interceptor.PrivilegetInterceptor"></interceptor>
</interceptors>
3.在拦截器类中,return “loginFail”,在struts.xml中配置结果集
4.降到配置的拦截器涉及到所要执行的方法上,action中配置
<interceptor-ref name="PrivilegetInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
默认的拦截器一定要写