0055 不能同時訪問靜態資源和動態資源的問題


剛才在折騰的時候,發現applicationContext.xml中配置了<mvc:default-servlet-handler />后,能訪問靜態資源,卻不能訪問動態資源了;注釋掉之后,不能訪問靜態卻能訪問動態資源。

找了些資料都說,如果把web.xml中將DispatcherServlet配置為攔截所有請求,包括靜態和動態的,像下面這樣:

    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>            <!-- 注意這,攔截所有請求 -->
  </servlet-mapping>

那么,在applicationContext中就得加上<mvc:default-servlet-handler />或者<mvc:resources 加些靜態資源映射配置/>,才能訪問到靜態資源。否則的話,web.xml中就得用類似於*.do或者*.html的方式配置<url-pattern>

我這樣配置了,卻導致靜態和動態資源不能同時訪問,最后還是搜索英文資源,才在stackoverflow上解決了問題:http://stackoverflow.com/questions/8023203/how-to-use-default-servlet-handler

原來applicationContext.xml中要加上<mvc:annotation-driven />配置。

原來applicationContext.xml里面用的:

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

配置的處理器映射器和處理器適配器,就沒用簡化的<mvc:annotation-driven />


最后:找資料,還得上Google找英文的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM