Struts2 静态资源映射

struts2框架中有一些它所需要的公共的静态内容,比如说js文件和一些css文件。当框架需要这些静态内容的时候,FilterDidpatcher会自动提供给我们。那么FilterDidpatcher是如何知道我们在请求静态内容的呢?任何请求只要以“/struts/”开头,那么FilterDidpatcher就会认为它是在请求静态内容。在识别出请求是请求静态资源后FilterDidpatcher如何去匹配寻找静态资源呢?这里有两个关键点:

1.确定所要请求的资源路径。FilterDidpatcher会截取/struts/后面的内容作为所要请求的资源。比如说现在请求是/struts/xhtml/styles.css,那么FilterDidpatcher就会把xhtml/styles.css作为我们所要请求的资源的路径:xhtml目录下面的styles.css文件。

2.到哪儿去寻找所请求的静态内容。默认情况下FilterDidpatcher会随意的org.apache.struts2.statictemplate这两个包中去寻找。如果我们还想在别的其它包中寻找静态内容的话,那就需要在web.xml配置中FilterDidpatcher时,给它添加一个参数”packages”,然后把它的值设置为一系列以逗号或者空格分隔的包名,如下面所示:

<filter>

       <filter-name>Struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

<init-param>

    <param-name>packages</param-name>

    <param-value>com.mangocity.static,hust.cm</param-value>

</init-param>

    </filter>

    <filter-mapping>

       <filter-name>Struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>






    描述:web应用下有一个目录“static”,现在要访问其中的“top.html”文件,即访问“localhost:8080/static/top.html”,服务器总是抱404错误。 

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

Java代码    收藏代码
  1. String resourcePath = RequestUtils.getServletPath(request);  
  2. if ("".equals(resourcePath) && null != request.getPathInfo()) {  
  3.     resourcePath = request.getPathInfo();  
  4. }  
  5. if (staticResourceLoader.canHandle(resourcePath)) {  
  6.     staticResourceLoader.findStaticResource(resourcePath, request, response);  
  7. else {  
  8.      // this is a normal request, let it pass through  
  9.      chain.doFilter(request, response);  
  10. }  
  11. // The framework did its job here  
  12. return;  


    其中,在DefaultStaticContentLoader类的canHandle方法中会对请求路径进行判断: 
   
Java代码    收藏代码
  1. public boolean canHandle(String resourcePath) {  
  2.     return serveStatic &&   
  3.       (resourcePath.startsWith("/struts") || resourcePath.startsWith("/static"));  
  4. }  

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

    然后,会进入DefaultStaticContentLoader类的findStaticResource方法,该方法的第一行语句是: 
   
Java代码    收藏代码
  1. String name = cleanupPath(path);  


    这里,cleanupPath方法的定义如下: 
   
Java代码    收藏代码
  1.  /** 
  2.  * @param path requested path 
  3.  * @return path without leading "/struts" or "/static" 
  4.  */  
  5. protected String cleanupPath(String path) {  
  6.     //path will start with "/struts" or "/static", remove them  
  7.     return path.substring(7);  
  8. }  


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

    总结:悲剧的错误,还以为是自己程序的bug,改了半天。需要加强对开源程序中具体实现的了解。 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值