在spring mvc中使用多个“mvc:resources”标签

时间:2011-03-06 02:34:23

标签: spring-mvc

我正在尝试使用spring v3.0.4中引入的新mvc标记库来提供静态资源。

我的春季配置看起来像这样

   <mvc:resources mapping="/scripts/**" location="/scripts/" />
   <mvc:resources mapping="/styles/**" location="/styles/" />
   <mvc:resources mapping="/images/**" location="/images/" />

但它正在向DispatcherController发送/ styles / **的请求。我正在使用此日志获取空白页。

2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <DispatcherServlet with name 'template' processing GET r
equest for [/template/styles/admin/struts-menu/menuExpandable.css]>

2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - <Matching patterns for request [/styles/ad
min/struts-menu/menuExpandable.css] are [/styles/**]>

2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - <URI Template variables for request [/styl
es/admin/struts-menu/menuExpandable.css] are {}>
2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - <Mapping [/styles/admin/struts-menu/menuEx
pandable.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1ce0390] and 3 inte
rceptors>
2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Last-Modified value for [/template/styles/admin/struts-
menu/menuExpandable.css] is: -1>
2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.resource.ResourceHttpRequestHandler] - <Trying relative path [admin/struts-me
nu/menuExpandable.css] against base location: ServletContext resource [/styles/]>

2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.resource.ResourceHttpRequestHandler] - <Found matching resource: ServletConte
xt resource [/styles/admin/struts-menu/menuExpandable.css]>

2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.resource.ResourceHttpRequestHandler] - <Determined media type [text/css] for
ServletContext resource [/styles/admin/struts-menu/menuExpandable.css]>
2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.resource.ResourceHttpRequestHandler] - <Resource not modified - returning 304
>

2011-03-05 21:05:11,923 DEBUG [com......template.web.admin.interceptors.SideMenuAdminInterceptorV2] - <Entering postHandle()>
2011-03-05 21:05:11,923 INFO [com.....template.web.admin.interceptors.SideMenuAdminInterceptorV2] - <ServletPath : /styles/admin/str
uts-menu/menuExpandable.css, ContextPath : /template, PathTranslated : null>
2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Null ModelAndView returned to DispatcherServlet with na
me 'template': assuming HandlerAdapter completed request handling>
2011-03-05 21:05:11,923 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Successfully completed request>

3 个答案:

答案 0 :(得分:8)

我知道这是一个非常古老的问题,但是今天我遇到了同样的情况,后来为我工作了......(希望对某人有帮助)

1。)我在下面添加了以下几行:

<mvc:resources location="/css/" mapping="css/**"/>
<mvc:resources location="/js/" mapping="js/**"/>
<mvc:resources location="/images/" mapping="images/**"/>

正好位于<mvc:annotation-driven />

之下

2.。)我将样式表和js文件链接起来(即与/ css /或/ js /不同)

<link href="css/styleIndex.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>

答案 1 :(得分:2)

文件夹:

/PROJECT/src/main/webapp/META-INF/static/img

/PROJECT/src/main/webapp/META-INF/static/css

/PROJECT/src/main/webapp/META-INF/static/js

dispatcher-servlet.xml:

<mvc:resources mapping="/static/**" location="/META-INF/static/" />
<mvc:resources mapping="/js/**" location="/META-INF/static/js/" />
<mvc:resources mapping="/img/**" location="/META-INF/static/img/" />
<mvc:resources mapping="/css/**" location="/META-INF/static/css/" />

如何加载:

<script type="text/javascript" src="<c:url value="/js/file.js" />"> </script>

答案 2 :(得分:0)

结帐spring-mvc-3.0.xsd:http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

可以将多个位置指定为以逗号分隔的列表,并按指定的顺序检查给定资源的位置。例如,值“/,classpath:/ META-INF / public-web-resources /”将允许从Web应用程序根目录和包含/ META-INF /的类路径上的任何JAR提供资源。 public-web-resources /目录,Web应用程序根目录中的资源优先。