I'm working on a Spring MVC 3 web application, I have some JSPs that works well with an exception, I'm having some problem displaying images -gif, jpeg and so on-. I tried these on the JSP side:
<img alt="TestDisplay" src="../images/XXX.gif" />
<img src="<c:url value=""/>images/XXX.gif" alt="TestDisplay"/>
<img src="<c:url value="/"/&g开发者_如何学运维t;images/XXX.gif" alt="TestDisplay"/>
<img src="<c:url value="../images/XXX.gif"/>" alt="TestDisplay"/>
<img src="<c:url value="/"/>WEB-INF/images/XXX.gif" alt="TestDisplay"/>
<img src="<c:url value="../WEB-INF/images/XXX.gif"/>" alt="TestDisplay"/>
<img src="<%=request.getContextPath()%>/images/XXX.gif" alt="TestDisplay" />
<img src="images/XXX.gif" alt="TestDisplay"/>
<img src="<spring:url value="/images/XXX.gif" htmlEscape="true" />" align="right" alt="TestDisplay"/>
But I'm not sure if the real problem comes from pages or anywhere. Any helpful answer will be rated.
Application Hierarchy Tree:
/webapp
//images
+XXX.gif
//WEB-INF
///images
+XXX.gif
Yes, I know images folder is duplicated, I've done it just to try any of the above options.
Thanks in advance
The correct usage is:
<img src="<c:url value="/images/XXX.gif" />" alt="TestDisplay"/>
Files in WEB-INF
are not accessible to clients.
you need to add to your applicationContext.xml
<mvc:resources mapping="/images/*" location="/images/" />
to tell Dispatcher Servlet that you will use resources from this location
or
<mvc:default-servlet-handler />
to use conteiner's default servlet for static resources handling.
more info about it - http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-default-servlet-handler
You will have to modify your code like below. This should work fine.
In xxx-servlet.xml
<context:component-scan base-package="com.asset" />
<mvc:resources mapping="/resources/**" location="/resources/" />
Add images or CSS folder inside resources e.g WebContent/resources/images/logo.png
Access the image using this statement
<img src="<%=request.getContextPath() %>/resources/images/logo.png"></a>
精彩评论