No mapping found for HTTP request with URI異常的緣由,的做用

1、最近作的一個項目有不少靜態資源文件,按照平時的配置springmvc進行配置發現訪問不到靜態文件,而且在我配置好controller去訪問結果仍是404html

   No mapping found for HTTP......前端

  1. 以前的springmvc配置java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.itwang.o2o.web"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/"/>
        <property name="suffix" value=".html"/>
    </bean>
    <!-- 資源映射 -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>
    <!-- 配置多部件解析器 須要引入file-upload和common-io jar包-->
    <!-- 定義文件上傳解析器 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 設定默認編碼 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 設定文件上傳的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880"></property>
    </bean>
</beans>

  2. web.xml 配置web

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1" metadata-complete="true">
    <display-name>myo2o_store</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 加載spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 解決post亂碼 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>myo2o_store</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myo2o_store</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我配置了DispatcherServlet攔截全部請求,由於配置了/。如今就連webapp下面的index.html文件都不能訪問,可是可以方位webapp下面的index.jsp文件,很使人費解spring

2、問題出現的緣由瀏覽器

  REST風格的資源URL不但願帶 .html 或 .do 等後綴.因爲早期的Spring MVC不能很好地處理靜態資源,因此在web.xml中配置DispatcherServlet的請求映射,每每使用 *.do 、 *.xhtml等方式。這就決定了請求URL必須是一個帶後綴的URL,而沒法採用真正的REST風格的URL。spring-mvc

  若是將DispatcherServlet請求映射配置爲"/",則Spring MVC將捕獲Web容器全部的請求,包括靜態資源的請求,Spring MVC會將它們當成一個普通請求處理,所以找不到對應處理器將致使錯誤。緩存

  如何讓Spring框架可以捕獲全部URL的請求,同時又將靜態資源的請求轉由Web容器處理,是可將DispatcherServlet的請求映射配置爲"/"的前提。因爲REST是Spring3.0最重要的功能之一,因此Spring團隊很看重靜態資源處理這項任務,給出了堪稱經典的兩種解決方案。服務器

2、如何解決問題mvc

  方法1.採用<mvc:default-servlet-handler />

<mvc:default-servlet-handler />

  在springMVC-servlet.xml中配置<mvc:default-servlet-handler />後,會在Spring MVC上下文中定義一個org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它會像一個檢查員,對進入DispatcherServlet的URL進行篩查,若是發現是靜態資源的請求,就將該請求轉由Web應用服務器默認的Servlet處理,若是不是靜態資源的請求,才由DispatcherServlet繼續處理。

  通常Web應用服務器默認的Servlet名稱是"default",所以DefaultServletHttpRequestHandler能夠找到它。若是你全部的Web應用服務器的默認Servlet名稱不是"default",則須要經過default-servlet-name屬性顯示指定:

  <mvc:default-servlet-handler default-servlet-name="所使用的Web服務器默認使用的Servlet名稱" />

  方法2.採用<mvc:resources />

  <mvc:default-servlet-handler />將靜態資源的處理經由Spring MVC框架交回Web應用服務器處理。而<mvc:resources />更進一步,由Spring MVC框架本身處理靜態資源,並添加一些有用的附加值功能。

  首先,<mvc:resources />容許靜態資源放在任何地方,如WEB-INF目錄下、類路徑下等,你甚至能夠將JavaScript等靜態文件打到JAR包中。經過location屬性指定靜態資源的位置,因爲location屬性是Resources類型,所以可使用諸如"classpath:"等的資源前綴指定資源位置。傳統Web容器的靜態資源只能放在Web容器的根路徑下,<mvc:resources />徹底打破了這個限制。

  其次,<mvc:resources />依據當前著名的Page Speed、YSlow等瀏覽器優化原則對靜態資源提供優化。你能夠經過cacheSeconds屬性指定靜態資源在瀏覽器端的緩存時間,通常可將該時間設置爲一年,以充分利用瀏覽器端的緩存。在輸出靜態資源時,會根據配置設置好響應報文頭的Expires 和 Cache-Control值。

  在接收到靜態資源的獲取請求時,會檢查請求頭的Last-Modified值,若是靜態資源沒有發生變化,則直接返回303相應狀態碼,提示客戶端使用瀏覽器緩存的數據,而非將靜態資源的內容輸出到客戶端,以充分節省帶寬,提升程序性能。

  在springMVC-servlet中添加以下配置:

<mvc:resources location="/,classpath:/META-INF/publicResources/" mapping="/resources/**"/>

  以上配置將Web根路徑"/"及類路徑下 /META-INF/publicResources/ 的目錄映射爲/resources路徑。假設Web根路徑下擁有images、js這兩個資源目錄,在images下面有bg.gif圖片,在js下面有test.js文件,則能夠經過 /resources/images/bg.gif 和 /resources/js/test.js 訪問這二個靜態資源。

假設WebRoot還擁有images/bg1.gif 及 js/test1.js,則也能夠在網頁中經過 /resources/images/bg1.gif 及 /resources/js/test1.js 進行引用。

相關文章
相關標籤/搜索