struts2–拦截器(非登录用户不可跳转至该页面)

  • Post author:
  • Post category:其他


拦截器类:

public class PrivilegeInterceptor extends MethodFilterInterceptor{
    //执行拦截器的方法
    @Override
    protected String doIntercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub
        //判断session中是否保存了后台用户的信息
        AdminUser adminUser = (AdminUser) ServletActionContext.getRequest().getSession().getAttribute("adminUser");
        if(adminUser == null) {
            //没有登录
            ActionSupport actionSupport =  (ActionSupport) arg0.getAction();
            actionSupport.addActionError("您还没有登录,没有访问权限!");
            return "loginfail";     
        }else {
            //已登录,允许执行
            return arg0.invoke();
        }

    }



}

struts.xml文件中配置:

<!-- 一定要写在上面,否则报错-->
<interceptors>
    <interceptor name="PrivilegeInterceptor"
    class="fjnu.lxf.myshop.interceptor.PrivilegeInterceptor"></interceptor>
</interceptors>

<global-results>
    <result name="loginfail">/admin/index.jsp</result>
</global-results>

<!-- 在需要拦截的Action里配置 例如:-->

<!-- 后台一级分类管理的Action -->
<action name="adminCategory_*" class="adminCategoryAction" method="{1}">
    <result name="findAll">/admin/category/list.jsp</result>
    <result name="savesuccess" type="redirectAction" >adminCategory_findAll.action</result>
    <result name="deletesuccess" type="redirectAction" >adminCategory_findAll.action</result>
    <result name="editsuccess">/admin/category/edit.jsp</result>
    <result name="updatesuccess" type="redirectAction" >adminCategory_findAll.action</result>
    <!-- 引入自定义的拦截器以及默认的拦截器 -->
    <interceptor-ref name="PrivilegeInterceptor"></interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>
</action>



版权声明:本文为deepquiet原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。