struts2应用中URL里面含有“static”时无法访问

今天在同事的应用出了一个小错误,与struts2有关,这里记录一下。

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

原因:在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错误。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值