servlet的url映射定義爲'/'表示映射所有路徑css
struts的過濾器是*.action,在spring mvc中設置成*.action或者*.do......也是能夠的,可是spring mvc就是霸氣,它就是要處理一切路徑.前端
對於靜態資源,在tomcat服務器上,必須經過servlet才能訪問,除此以外別無他法.java
即使是你本身不寫servlet來訪問靜態資源,tomcat不管如何也要找到一個servlet來處理靜態資源,因此別以爲用servlet處理會慢,不管如何都要用servlet來處理.jquery
一言以蔽之,在tomcat容器中,處理資源的基本單位是servlet,進行數據操做計算的基本單位也是servlet.nginx
前人之述備矣,天下文章一大抄.web
Spring MVC 中的核心 servlet - DispatcherServlet,咱們在 web.xml 文件中一般這樣定義:spring
<servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/conf/spring/mvc-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
很明顯,該 servlet 對應的 url-pattern 定義成 /,所以該 servlet 會匹配上諸如 /images/a.jpg, /css/hello.css 等這些靜態資源,甚至包括 /jsp/stock/index.jsp 這些 jsp 也會匹配。可是並無定義相應的 Controller 來處理這些資源,所以這些請求一般是沒法完成的。
很麻煩?沒有 Struts 方便?此言差矣,所以你在 Struts 定義其核心 filter 的 url-pattern 是 *.action,固然不會對處理 jsp 和靜態資源操做影響了。Spring MVC 若也定義相似的 url-pattern,一樣不存在問題。
說到這裏,咱們應該想一個問題。Tomcat 中,只有 servlet 可以處理請求,即便是 jsp,也會被編譯成 servlet。咱們即使使用 Struts,定義 *.action 的url-pattern,那 .css, *.gfi 等這些靜態資源究竟是誰來處理了???你可不要想固然的認爲我不是輸入了圖片的路徑了嗎?如,/images/a/b/c.gif。請注意,servlet 容器中,只有 servlet 採用處理資源!
由 servlet 處理這些資源那是必定了。不過,不一樣的 servlet 容器/應用服務器,處理這些靜態資源的 servlet 的名字不大同樣: apache
◇ 方案一:激活 Tomcat 的 defaultServlet 來處理靜態資源緩存
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping>
每種類型的靜態資源須要分別配置一個 servlet-mapping,同時,要寫在 DispatcherServlet 的前面, 讓 defaultServlet 先攔截。
◇ 方案二:Spring 3.0.4 之後版本提供了 <mvc:resources />tomcat
<mvc:resources location="/resources/" mapping="/resources/**"/>
/resources/** 映射到 ResourceHttpRequestHandler 進行處理,location 指定靜態資源的位置,能夠是 web application 根目錄下、jar 包裏面,這樣能夠把靜態資源壓縮到 jar 包中。cache-period 可使得靜態資源進行 web cache。
使用 <mvc:resources /> 元素,會把 mapping 的 URI 註冊到 SimpleUrlHandlerMapping 的 urlMap 中,key 爲 mapping 的 URI pattern 值,而 value 爲 ResourceHttpRequestHandler,這樣就巧妙的把對靜態資源的訪問由 HandlerMapping 轉到 ResourceHttpRequestHandler 處理並返回,因此就支持 classpath 目錄, jar 包內靜態資源的訪問。
◇ 方案三:使用 <mvc:default-servlet-handler />
<mvc:default-servlet-handler /> 會把 "/**" url 註冊到 SimpleUrlHandlerMapping 的 urlMap 中,把對靜態資源的訪問由 HandlerMapping 轉到 org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler 處理並返回。DefaultServletHttpRequestHandler 使用就是各個 Servlet 容器本身的默認 Servlet。
補充說明下以上提到的 HandlerMapping 的 order 的默認值:
Spring 會先執行 order 值比較小的。當訪問一個 a.jpg 圖片文件時,先經過 DefaultAnnotationHandlerMapping 來找處理器,必定是找不到的,咱們沒有叫 a.jpg 的 Controller。再按 order 值升序找,因爲最後一個 SimpleUrlHandlerMapping 是匹配 "/**" 的,因此必定會匹配上,再響應圖片。
Spring MVC 中,訪問一個圖片,還要走層層匹配。性能確定好不到哪裏去。不只僅是 Spring MVC,即使 Struts,它們畢竟存活於 servlet 容器,只要由 servlet 容器處理這些靜態資源,必然要將這些資源讀入 JVM 的內存區中。因此,處理靜態資源,咱們一般會在前端加 apache 或 nginx。
其中處理靜態資源的類是org.springframework.web.servlet.resource.ResourceHttpRequestHandler,並且在location的描述中說明Each location must point to a valid directory. 即每一個location都必須指向一個有效的目錄。
下面看一下org.springframework.web.servlet.resource.ResourceHttpRequestHandler中是如何處理靜態資源請求的:
首先它實現了org.springframework.web.HttpRequestHandler這個接口的handleRequest方法:
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 這裏根據配置來設置緩存的header checkAndPrepare(request, response, true); // 獲取要獲取的資源,若是不存在,直接返回404錯誤 Resource resource = getResource(request); if (resource == null) { logger.debug("No matching resource found - returning 404"); response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // 省略部分代碼…… // 返回相應的資源,即把資源文件寫到響應中 writeContent(response, resource); }
而getResource方法實現以下:
protected Resource getResource(HttpServletRequest request) { String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); // 省略部分處理和檢驗路徑的代碼…… // 這裏循環從配置的location下查找請求的靜態資源 for (Resource location : this.locations) { try { if (logger.isDebugEnabled()) { logger.debug("Trying relative path [" + path + "] against base location: " + location); } Resource resource = location.createRelative(path); // 判斷資源存在並且可以讀取 if (resource.exists() && resource.isReadable()) { // 這裏就是3.2.12及之後處理上的差異,在3.2.12前,不會判斷該資源是否在指定的路徑下,直接就返回了resource,而3.2.12及之後作了以下判斷 if (isResourceUnderLocation(resource, location)) { if (logger.isDebugEnabled()) { logger.debug("Found matching resource: " + resource); } return resource; } else { if (logger.isTraceEnabled()) { logger.trace("resource=\"" + resource + "\" was successfully resolved " + "but is not under the location=\"" + location); } return null; } } else if (logger.isTraceEnabled()) { logger.trace("Relative resource doesn't exist or isn't readable: " + resource); } } catch (IOException ex) { logger.debug("Failed to create relative resource - trying next resource location", ex); } } return null; }
下面看看isResourceUnderLocation的實現:
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (!resource.getClass().equals(location.getClass())) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = location.getURL().toExternalForm(); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = ((ClassPathResource) location).getPath(); } else if (resource instanceof ServletContextResource) { resourcePath = ((ServletContextResource) resource).getPath(); locationPath = ((ServletContextResource) location).getPath(); } else { resourcePath = resource.getURL().getPath(); locationPath = location.getURL().getPath(); } // 這裏是對路徑的處理,若是咱們配置的是/res/**,那麼直接拼接成了/res/**/,若是請求資源爲/res/jquery.js,
//那麼會判斷res/jquery.js是否以/res/**/開頭,若是不是,則返回該location下沒有該資源,致使不能404錯誤 locationPath = (locationPath.endsWith("/") || !StringUtils.hasLength(locationPath) ? locationPath : locationPath + "/"); if (!resourcePath.startsWith(locationPath)) { return false; } if (resourcePath.contains("%")) { // Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars... if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) { if (logger.isTraceEnabled()) { logger.trace("Resolved resource path contains \"../\" after decoding: " + resourcePath); } return false; } } return true; }
'/'表示根目錄,以'/'結尾的路徑表示文件夾,location爲'/'表示該路徑下的所有文件至關於'/**','/*'表示單級目錄,因此'/'=='/**'>'/*'
mvc:resource能夠設置多個屬性
在@RequestMapping註解中,若是沒有參數,則表示這個servlet是默認的servlet,會處理一切資源.
<!-- 對靜態資源文件的訪問 方案一 --> <mvc:default-servlet-handler/> <!-- 對靜態資源文件的訪問 方案二 --> <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
<!-- 對靜態資源文件的訪問 方案二的簡便寫法-->
<mvc:resources location="/" mapping="/" />