开发者

Use of multiple "mvc:resources" tag in spring mvc

开发者 https://www.devze.com 2023-02-14 21:33 出处:网络
I am trying to serve static resources using new mvc tag library introduced in spring v3.0.4. My spring config looks like this

I am trying to serve static resources using new mvc tag library introduced in spring v3.0.4.

My spring config looks like this

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

But It is sending request for /styles/** to DispatcherController. I am getting blank page with this log.

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>


I know its a very old question, but today I faced this same scenario and following worked for me.. (hope it will help to someone)

1.) I added below lines:

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

just below the <mvc:annotation-driven />

2.) I linked my stylesheet and js files like this (i.e. unlike /css/ or /js/)

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


folder :

/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/" />

How to load :

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


checkout the spring-mvc-3.0.xsd : http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

Multiple locations may be specified as a comma-separated list, and the locations will be checked for a given resource in the order specified. For example, a value of "/, classpath:/META-INF/public-web-resources/" will allow resources to be served both from the web app root and from any JAR on the classpath that contains a /META-INF/public-web-resources/ directory, with resources in the web app root taking precedence.

0

精彩评论

暂无评论...
验证码 换一张
取 消