Using multiple "mvc: resources" tags in spring mvc

I am trying to use static resources using the 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 sends a request for / styles / ** to the DispatcherController. I get a blank page with this magazine.

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
source share
3 answers

I know his very old question, but today I came across the same scenario, and after that worked for me .. (I hope this helps someone)

1.) I added the lines below:

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

slightly lower <mvc:annotation-driven />

2.) I linked my stylesheets and js 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>
+8
source

:

/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 download:

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

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

Multiple locations can be specified as a comma-separated list, and locations will be checked for a given resource in that order. For example, the value "/, classpath: / META-INF / public-web-resources /" will allow you to provide resources both from the root of the web application, and from any JAR in the class path that contains / META -INF / public-web -resources /, with resources at the root of the web application taking precedence.

-1
source

Source: https://habr.com/ru/post/1796576/


All Articles