spring-MVC访问静态资源
Since I spent a lot of time on this issue, I thought I'd share my solution. Since spring 3.0.4, there is a configuration parameter that is called <mvc:resources/>
(more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site's root.
In order to use this, use a directory structure that looks like the following:
src/ springmvc/ web/ MyController.java WebContent/ resources/ img/ image.jpg WEB-INF/ jsp/ index.jsp web.xml springmvc-servlet.xml
The contents of the files should look like:
src/springmvc/web/HelloWorldController.java:
1 package springmvc.web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 publicclassHelloWorldController{ 8 9 @RequestMapping(value="/") 10 publicString index(){ 11 return"index"; 12 } 13 }
WebContent/WEB-INF/web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 <servlet> 8 <servlet-name>springmvc</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>springmvc</servlet-name> 15 <url-pattern>/</url-pattern> 16 </servlet-mapping> 17 </web-app>
WebContent/WEB-INF/springmvc-servlet.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beansxmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 11 12 <!-- not strictly necessary for this example, but still useful, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more information --> 13 <context:component-scanbase-package="springmvc.web"/> 14 15 <!-- the mvc resources tag does the magic --> 16 <mvc:resourcesmapping="/resources/**"location="/resources/"/> 17 18 <!-- also add the following beans to get rid of some exceptions --> 19 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 20 <bean 21 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 22 </bean> 23 24 <!-- JSTL resolver --> 25 <beanid="viewResolver" 26 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 27 <propertyname="viewClass" 28 value="org.springframework.web.servlet.view.JstlView"/> 29 <propertyname="prefix"value="/WEB-INF/jsp/"/> 30 <propertyname="suffix"value=".jsp"/> 31 </bean> 32 33 </beans>
WebContent/jsp/index.jsp:
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <h1>Page with image</h1> 3 <!-- use c:url to get the correct absolute path --> 4 <img src="<c:urlvalue="/resources/img/image.jpg"/>" />
Hope this helps :-)