linmeimeimei 2016-09-08 03:38 采纳率: 0%
浏览 941

菜鸟自学Struts2 拦截器不起作用,求大神帮忙带飞

进入jsp页面后访问没有进过拦截器拦截 直接进去action的处理类 配置看了好久 找不出来哪里错了 求各位大神指点迷津。。。。

自定义拦截器: 登录即可查询 没登录查询不了 登录后不是admin 无法进行商品操作
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {

    // 得到被拦截的actionContext所有对象
    ActionContext actionContext = actionInvocation.getInvocationContext();

    // 从session中获得role角色对象
    String role = (String) actionContext.getSession().get("role");

    System.out.println(role);

    // 得到request对象
    Map request = (Map) actionContext.get("request");

    if (role == null) {

        request.put("msg", "没有登录,请<a href = 'index.jsp'>重新登录</a>");
        return "error";

    } else {

        String method = actionInvocation.getProxy().getMethod()
                .toLowerCase();
        if (!role.equals("admin")
                && (method.contains("add") || method.contains("del") || method
                        .contains("update"))) {

            request.put("msg", "不是管理员,没有权限进行这项操作   <a href = 'javascript:history.go(-1)'>返回</a>");

            return "error";

        }

    }
    return actionInvocation.invoke();

}



jsp页面 没有进过拦截 直接调用了action处理类的对应方法  

 <body>

<h2>订单管理系统操作功能列表

    <hr/>
    <ul><a href= "cust!queryCust.action">查询客户</a></ul>
    <ul><a href= "cust!addCust.action">添加客户</a></ul>
    <ul><a href= "shop!addShop.action">添加商品</a></ul>
    <ul><a href= "shop!updateShop.action">修改商品</a></ul>


</h2>   

Struts.xml




<package name="intercepter" extends="struts-default">
    <interceptors>
        <interceptor name="auth" class="com.intercepter.MyIntercepter"></interceptor>
    </interceptors>

    <default-interceptor-ref name="auth"/>

    <global-results><result name = "error">/error.jsp</result></global-results>

    <action name = "cust" class = "com.action.CustAction">
    </action>
    <action name = "shop" class = "com.action.ShopAction">
    </action>
</package>
  • 写回答

1条回答

  • 兜茄破 2016-09-08 09:36
    关注

    拦截器只声明还不行,哪些action需要用它拦截,还是需要引用一下,例如下面这样:

     <action name="cust" class="com.action.CustAction">          
               <!-- 拦截器配置 -->
               <interceptor-refname="auth"></interceptor-ref>
                        <interceptor-refname="defaultStack"></interceptor-ref>
      </action>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?