I'm having some problems with the tag (Spring 3.0.5). I want to add images to my web application, but it doesnt work.
Here is part of my beans config:
<mvc:annotation-driven/>
<mvc:default-servlet-handler default-servlet-name="ideafactory"/>
<mvc:resources mapping="/resources/**" location="/, classpath:/WEB-INF/public-resources/" cache-period="10000" />
Trying to add an image in a jsp file:
<img src="<c:url value="/resources/logo.png" />" alt="Idea Factory" />
First of all, I don't know really where to store the resources (src/main/resources/public-resources? src/main/webapp/WEB-INF/public-resources?). Secondly, this config does not work, I can't see the image. What's wrong?
Thanks!
EDIT: the solution given here: Spring Tomcat and static resources and mvc:resources doesn't work either... Added without success.
EDIT 2: I tried to remove the mvc:resource tag and let only the mvc:default-servlet-handler> one, gave me infinite loop and stackoverflow... o_O (Serving static content with Spring 3)
Recommendations for resources in order to handle HTTP GET requests for /resources/** by offering static resources in the ${webappRoot}/resources directory is to simply add the following line in the configuration file:
It has worked for me.
Sources (Spring in Action book and http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html)
Found the error:
Final xxx-servlet.xml config:
Image in src/webapp/resources/logo.png
Works!
It works for me:
Put the resources under:
src/main/webapp/images/logo.png
and then access them via/resources/images/logo.png
.In the
war
they will be then located atimages/logo.png
. So the first location (/
) formmvc:resources
will pick them up.The second location (
classpath:/WEB-INF/public-resources/
) inmvc:resources
(looks like you used some roo based template) can be to expose resources (for example js-files) form jars, if they are located in the directoryWEB-INF/public-resources
in the jar.@Nanocom's answer works for me. It may be that lines have to be at the end, or could be because has to be after of some the
bean
class like this:As said by @Nancom
So for clarity lets our image is in
The location attribute of the tag defines the base directory location of static resources that you want to serve. It can be images path that are available under the
src/main/webapp/resources/images/
directory; you may wonder why we have given only /resources/ as the location value instead of src/main/webapp/resources/images/. This is because we consider the resources directory as the base directory for all resources, we can have multiple subdirectories under resources directory to put our images and other static resource files.The second attribute, mapping, just indicates the request path that needs to be mapped to this resource directory. In our case, we have assigned
/resources/**
as the mapping value. So, if any web request starts with the/resource
request path, then it will be mapped to the resources directory, and the/**
symbol indicates the recursive look for any resource files underneath the base resource directory.So for url like
http://localhost:8080/webstore/resource/images/logo.png
. So, while serving this web request, Spring MVC will consider/resource/images/logo.png
as the request path. So, it will try to map/resource
to the resource base directory, resources. From this directory, it will try to look for the remaining path of the URL, which is/images/logo.png
. Since we have the images directory under the resources directory, Spring can easily locate the image file from the images directory.So
gives us for given [requests] -> [resource mapping]:
http://localhost:8080/webstore/resource/images/logo.png
-> searches inresource/images/logo.png
http://localhost:8080/webstore/resource/images/small/picture.png
-> searches inresource/images/small/picture.png
http://localhost:8080/webstore/resource/css/main.css
-> searches inresource/css/main.css
http://localhost:8080/webstore/resource/pdf/index.pdf
-> searches inresource/pdf/index.pdf