spring mvc訪問靜態文件(css/js/img)訪問不到

web.xml中配置:css

 <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>web

分析緣由:spring

由於mapping使用的是/ 默認攔截全部。spring-mvc

解決方案:mvc

在spring-mvc的配置文件中添加:app

 <mvc:default-servlet-handler/>url

 <mvc:default-servlet-handler/>
    <mvc:annotation-driven />
      <!-- 處理靜態資源的請求 -->
    <mvc:resources location="/resources/" mapping="/resources/**" />
    
    <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" />
  提示:spa

若是添加了 <mvc:default-servlet-handler/>提示錯誤信息xml

The prefix "mvc" for element "mvc:annotation-driven" is not bound.element

解決方案:

在bean添加以下:

 xmlns:mvc="http://www.springframework.org/schema/mvc" 
  

在 xsi:schemaLocation中添加:

 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

相關文章
相關標籤/搜索