您的位置:首页 > 编程语言 > Java开发

struts中的拦截器验证登录

2012-10-20 22:27 537 查看
权限验证拦截器PermissionInterceptor
//继承了AbstractInterceptor只需要重写intercept方法就可以了,省去了init()和destory()方法
public class PermissionInterceptor extends AbstractInterceptor
{
//验证用户的权限
@Override
public String intercept(ActionInvocation arg0) throws Exception
{
if(ActionContext.getContext().getSession().get("admin")==null)
{
//用户没有登入,保存提示信息到request中
ServletActionContext.getRequest().setAttribute("message","未登入,请先登入!");
//ActionContext.getContext().put("message","未登入,请先登入!");
return "nologin";
}
//继续执行后续的action
returnarg0.invoke();
}
}


Struts.xml配置

//因为验证是否登入是共用的,所以放在基包base中,在使用的时候必须要对添加的拦截器进行声明,并且还要将默认的拦截器加入不然就会失去默认拦截器的效果,

<package name="base" extends="struts-default">
<!-- 拦截器的配置 -->
<interceptors>
<interceptor name="permissionInterceptor" class="interceptor.PermissionInterceptor"/> //声明
<interceptor-stack name="allInterceptor">
<!-- 必须包含原有的默认的defaultStack才会生效 -->
<interceptor-ref name="defaultStack"/> //默认拦截器栈
<interceptor-ref name="permissionInterceptor"/>  //引用
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="allInterceptor"/> //默认拦截器设置
<global-results>
<result name="ajax">/ajax.jsp</result>
<result name="err">/err.jsp</result>  <!-- 出现错误跳转到err.jsp -->
<result name="nologin">/login.jsp</result>  <!-- 没有登陆就跳转到login.jsp -->
</global-results>
</package>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: