2011-03-06 74 views
3

我正尝试使用Spring v3.0.4中引入的新mvc标记库来提供静态资源。在春天使用多个“mvc:resources”标签mvc

我的Spring配置看起来像这样

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

,但它在发送请求/风格/ **到DispatcherController。我得到这个日志的空白页面。

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.[email protected]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> 
+0

当你说“发送/ styles/**的请求”时,你实际上没有在请求中放入'/ styles/**',是吗? – skaffman 2011-03-08 08:42:05

+0

没有@skaffman。我的意思是,html页面有样式链接。这是个老问题,我甚至不记得我是否确实解决了。 – 2012-12-31 03:09:27

回答

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> 
+0

+10解决我的问题:) – 2015-05-28 06:55:20

+0

很高兴帮助:) – agpt 2015-05-28 10:11:35

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 

调度-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> 
-1

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

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

+0

这个问题不是关于多个位置,而是多个映射。 – 2015-08-11 23:04:31