zoukankan      html  css  js  c++  java
  • Struts2中默认对以/struts、/static开头URL的处理

    今天试着配置弄个权限,刚一开始就把我卡住了,在web.xml时使用了*.jspx一切正常,可当我将其改为/*全部guolv时发现页面不能加载js、css了,那个郁闷,于是google了下找到答案:“The Dojo files are shipped with Struts 2.0.x within struts2-core-2.0.x.jar. When a request for a resource is received that starts with the /struts URL, the FilterDispatcher serves the corresponding file in struts2-core-2.0.x.jar:/org/apache/struts2/static/. This feature needs to be turned off to use an alternative Dojo profile.凡是用/struts开头的URL,struts2的过滤器都会到struts2-core-2.0.x.jar:/orgapache/struts2/static/下面去找资源。只要在struts.properties文件中更改一个属性,就可以关闭掉这种默认的特性。struts.serve.static=false 。”于是照着更改后,一切就OK了。

    也可以将web.xml的url过滤改为*.xxx(xxx为action后缀名)或将static、struts改为其他名称(如resources)来避免这个问题

    另见http://caoxudong818.iteye.com/blog/1137571如下:

    原因:在struts2的FilterDispatcher类的doFilter方法中,如果请求的是静态资源,struts2会判断该请求是否可以处理,这里的代码如下:

    String resourcePath = RequestUtils.getServletPath(request);  
    if ("".equals(resourcePath) && null != request.getPathInfo()) {  
        resourcePath = request.getPathInfo();  
    }  
    if (staticResourceLoader.canHandle(resourcePath)) {  
        staticResourceLoader.findStaticResource(resourcePath, request, response);  
    } else {  
         // this is a normal request, let it pass through  
         chain.doFilter(request, response);  
    }  
    // The framework did its job here  
    return;  

    其中,在DefaultStaticContentLoader类的canHandle方法中会对请求路径进行判断:

    public boolean canHandle(String resourcePath) {  
        return serveStatic &&   
          (resourcePath.startsWith("/struts") || resourcePath.startsWith("/static"));  
    }  

     这里,serveStatic的值为true,再加上要访问的资源以“/static”开头,所以这里返回true。 

        然后,会进入DefaultStaticContentLoader类的findStaticResource方法,该方法的第一行语句是:

    String name = cleanupPath(path); 

     这里,cleanupPath方法的定义如下: 

    /** 
     * @param path requested path 
     * @return path without leading "/struts" or "/static" 
     */  
    protected String cleanupPath(String path) {  
        //path will start with "/struts" or "/static", remove them  
        return path.substring(7);  
    }  

     struts2把“/static”截掉了,这样,后面再进行解析的时候,就变成了解析对“/top.html”的请求,所以会报404错误。

  • 相关阅读:
    Linux命令应用大词典-第11章 Shell编程
    Kubernetes 学习12 kubernetes 存储卷
    linux dd命令
    Kubernetes 学习11 kubernetes ingress及ingress controller
    Kubernetes 学习10 Service资源
    Kubernetes 学习9 Pod控制器
    Kubernetes 学习8 Pod控制器
    Kubernetes 学习7 Pod控制器应用进阶2
    Kubernetes 学习6 Pod控制器应用进阶
    Kubernetes 学习5 kubernetes资源清单定义入门
  • 原文地址:https://www.cnblogs.com/jice/p/2471313.html
Copyright © 2011-2022 走看看