2011-03-06 71 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