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; public boolean canHandle(String resourcePath) { return serveStatic && (resourcePath.startsWith("/struts") || resourcePath.startsWith("/static")); } String name = cleanupPath(path);
/** * @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); }