spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...


问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...


web.xml下对spring的DispatcherServlet请求url映射的配置,原配置如下:


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

原因:<servlet-mapping>的<url-pattern>/</url-pattern>把所有的请求都交给spring去处理了,而所有available的请求url都是在Constroller里使用类似@RequestMapping(value = "http://www.Linuxso.com/login/{user}", method = RequestMethod.GET)这样的注解配置的,这样的话对js/css/jpg/gif等静态资源的访问就会得不到。

解决方法1:在web.xml里添加如下的配置

<servlet-mapping>
     <servlet-name>default</servlet-name>
     <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>

</servlet-mapping>
   
<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>



解决方法2:在spring的配置文件中添加如下一行:

<mvc:default-servlet-handler/>

注意,需要是spring3.0.5以上版本


解决方法3

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

<mvc:resources mapping="/resources/**" location="/resources/" />
 
这个配置告诉spring 静态资源的处理方式

你可能感兴趣的:(resource)