Spring MVC app中的静态资源

时间:2011-11-22 10:10:35

标签: spring spring-mvc

在我的Spring mvc应用程序中,我想使用mvc:resources提供静态资源。

我的web.xml映射看起来:

<servlet-mapping>
 <servlet-name>main</servlet-name>
 <url-pattern>/</url-pattern>       
</servlet-mapping>

其中main是调度程序servlet以提供所有内容

在我的servlet.xml文件中,我添加了:

<mvc:resources mapping="/static/**" location="/static/"/>

当我的应用程序上下文为空时(例如localhost:8080 /),它可以正常工作,但是当我在另一个上下文中部署应用程序时,它不起作用,我得到了404。

我尝试了很多组合:

"static/**"
"*/static/**"

没有任何作用。

我确定这是服务器上下文问题,但我不知道(我也无法在Google中找到解决方案)如何解决这个问题。请帮忙。

2 个答案:

答案 0 :(得分:0)

我能够使用以下约定成功映射我的静态资源:

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

答案 1 :(得分:0)

我们工作的最简单方法是在 servlet-config.xml (在web.xml中配置为contextConfigLocation的文件)中添加以下内容:

<mvc:default-servlet-handler/>
相关问题