Spring MVC 直接访问静态文件(html、jpg、js...)

时间:2020-12-03 13:05:45
首先在spring-servle.xml文件中加入如下代码: 

Xml代码  Spring MVC 直接访问静态文件(html、jpg、js...)
  1. <mvc:default-servlet-handler/>  


最后在web.xml中加入如下代码即可 

Xml代码  Spring MVC 直接访问静态文件(html、jpg、js...)
  1. <servlet-mapping>  
  2.         <servlet-name>default</servlet-name>  
  3.         <url-pattern>*.css</url-pattern>  
  4.     </servlet-mapping>  
  5.     <servlet-mapping>  
  6.         <servlet-name>default</servlet-name>  
  7.         <url-pattern>*.js</url-pattern>  
  8.     </servlet-mapping>  
  9.     <servlet-mapping>  
  10.         <servlet-name>default</servlet-name>  
  11.         <url-pattern>*.ico</url-pattern>  
  12.     </servlet-mapping>  
  13.     <servlet-mapping>  
  14.         <servlet-name>default</servlet-name>  
  15.         <url-pattern>*.gif</url-pattern>  
  16.     </servlet-mapping>  
  17.     <servlet-mapping>  
  18.         <servlet-name>default</servlet-name>  
  19.         <url-pattern>*.jpg</url-pattern>  
  20.     </servlet-mapping>  
  21.     <servlet-mapping>  
  22.         <servlet-name>default</servlet-name>  
  23.         <url-pattern>*.png</url-pattern>  
  24.     </servlet-mapping>  
  25.     <servlet-mapping>  
  26.         <servlet-name>default</servlet-name>  
  27.         <url-pattern>*.bmp</url-pattern>  
  28.     </servlet-mapping>  
  29.     <servlet-mapping>  
  30.         <servlet-name>default</servlet-name>  
  31.         <url-pattern>*.jpeg</url-pattern>  
  32.     </servlet-mapping>  
  33.     <servlet-mapping>  
  34.         <servlet-name>default</servlet-name>  
  35.         <url-pattern>*.swf</url-pattern>  
  36.     </servlet-mapping>  
  37.     <servlet-mapping>  
  38.         <servlet-name>default</servlet-name>  
  39.         <url-pattern>*.flv</url-pattern>  
  40.     </servlet-mapping>  
  41.     <servlet-mapping>  
  42.         <servlet-name>default</servlet-name>  
  43.         <url-pattern>*.xml</url-pattern>  
  44.     </servlet-mapping>  
  45.     <servlet-mapping>  
  46.         <servlet-name>default</servlet-name>  
  47.         <url-pattern>*.txt</url-pattern>  
  48.     </servlet-mapping>  
  49.     <servlet-mapping>  
  50.         <servlet-name>default</servlet-name>  
  51.         <url-pattern>*.htm</url-pattern>  
  52.     </servlet-mapping>  
  53.     <servlet-mapping>  
  54.         <servlet-name>default</servlet-name>  
  55.         <url-pattern>*.html</url-pattern>  
  56.     </servlet-mapping>