我正在开发一个spring-mvc应用程序,该应用程序需要从webapp文件夹和文件系统加载静态位置。我关注了documentation,this答案和更多教程,并配置了以下内容。
<mvc:resources mapping="/images/**" location="/img/" cache-period="10000"/>
<mvc:resources mapping="/resources/**" location="file://D:/my-apps/sample-mvc/images/" cache-period="10000"/>
<mvc:annotation-driven/>
目录结构如下。
web.xml->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name> <!-- Configuration location for servlet context -->
<param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</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>*.png</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>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff2</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name> <!-- Configuration location for main application context -->
<param-value>
classpath:application-context.xml
</param-value>
</context-param>
但是在给定的两个URL中没有提供任何文件。 (本地主机:8080 / sample-mvc-app / images / a1-img-001.jpg,或本地主机:8080 / sample-mvc-app / resources / test-uploaded-img-1.jpg)
进一步测试,我发现,如果我使用原始目录名称的URL(localhost:8080 / sample-mvc-app / img / a1-img-001.jpg),图像将加载到浏览器中。 / p>
有人可以帮我找出这里的问题吗?
另外, Servlet容器:tomcat
答案 0 :(得分:0)
尝试从web.xml中删除默认的servlet映射。 Default servlet是在每个Web应用程序中创建的Servlet。在tomcat it serves static resources中。