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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值