Reputation: 3726
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)
Upvotes: 24
Views: 128907
Reputation: 17198
As said by @Nancom
<mvc:resources location="/resources/" mapping="/resources/**"/>
So for clarity lets our image is in
resources/images/logo.png"
The location attribute of the mvc:resources 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 sub-directories 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 resources
directory. In our case, we have assigned /resource/**
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 resources
directory.
So for url like
http://localhost:8080/webstore/resources/images/logo.png
. So, while serving this web request, Spring MVC will consider /resources/images/logo.png
as the request path. So, it will try to map /resources
to the base directory specified by the location attribute, 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
<mvc:resources location="/resources/" mapping="/resources/**"/>
gives us for given [requests] -> [resource mapping]:
http://localhost:8080/webstore/resource/images/logo.png
-> searches in resources/images/logo.png
http://localhost:8080/webstore/resources/images/small/picture.png
-> searches in resources/images/small/picture.png
http://localhost:8080/webstore/resources/css/main.css
-> searches in resources/css/main.css
http://localhost:8080/webstore/resources/pdf/index.pdf
-> searches in resources/pdf/index.pdf
Upvotes: 7
Reputation: 11
This worked for me
In JSP, to view the image
<img src="${pageContext.request.contextPath}/resources/images/slide-are.jpg">
In dispatcher-servlet.xml
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
Upvotes: 0
Reputation: 3121
You can keep rsouces directory in Directory NetBeans: Web Pages Eclipse: webapps
File: dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<mvc:resources location="/resources/theme_name/" mapping="/resources/**" cache-period="10000"/>
<mvc:annotation-driven/>
</beans>
File: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
In JSP File
<link href="<c:url value="/resources/css/default.css"/>" rel="stylesheet" type="text/css"/>
Upvotes: 1
Reputation: 21
@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:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler" />
<mvc:resources mapping="/resources/**"
location="/resources/"
cache-period="10000" />
Upvotes: 0
Reputation: 22440
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:
<resources mapping="/resources/**" location="/resources/" />
It has worked for me.
Sources (Spring in Action book and http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html)
Upvotes: 5
Reputation: 81
I also met this problem before. My situation was I didn't put all the 62 spring framework jars into the lib file (spring-framework-4.1.2.RELEASE edition), it did work. And then I also changed the 3.0.xsd into 2.5 or 3.1 for test, it all worked out. Of course, there are also other factors to affect your result.
Upvotes: 0
Reputation: 120761
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
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 at images/logo.png
. So the first location (/
) form mvc:resources
will pick them up.
The second location (classpath:/WEB-INF/public-resources/
) in mvc: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 directory WEB-INF/public-resources
in the jar.
Upvotes: 25
Reputation: 99
It works for me:
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
Upvotes: 3
Reputation: 31
Different order make it works :)
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
Upvotes: 3
Reputation: 3726
Found the error:
Final xxx-servlet.xml config:
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
Image in src/webapp/resources/logo.png
Works!
Upvotes: 28