0

I have code as follows:

<td width="24px">
    <img src="${pageContext.request.contextPath}/images/success.gif"/>
</td>

My gif file is located at location

\src\main\webapp\images

It doesn't load the images when I try to load it by using Spring MVC framework.

I tried by giving the path

<img src="images/success.gif/">

<img src="webapp/images/success.gif/">

View Source gives:

 <img src="/MyProject-UX/images/success.gif">

My spring config file:

<context:component-scan base-package="com.mycompany"/>  
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/view/"/>
       <property name="suffix" value=".jsp"/>
</bean>

@Controller    
@RequestMapping("/closeWindow")
public class ClosingWindow {
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {     
       return "closeWindow";
   }

I have followed guide here Link-1 Link-2

Community
  • 1
  • 1
AKB
  • 5,918
  • 10
  • 53
  • 90
  • so what does the generated html look like? and what would you have to put into the `src` to make it load that image? That'll tell you what you need to do to fix the code. – Marc B Oct 22 '14 at 19:05
  • What is the `success` bean? What does its `getGif()` method return? You probably want `${pageContext.request.contextPath}/images/success.gif`. – JB Nizet Oct 22 '14 at 19:24
  • EDIT: but you didn't update the output. There's no way the updated code produces the given output. You're still missing a `/`. – JB Nizet Oct 22 '14 at 19:29
  • @JBNizet, again I have updated my code above. – AKB Oct 22 '14 at 19:32
  • OK. So what's the problem. The generated HTML is correct now. Is the image in the right location? Is the dispatcher servlet mapped to `/`? – JB Nizet Oct 22 '14 at 19:39
  • @JBNizet, my image is located at src/main/webapp/images and I am using Spring MVC to load the JSP pages. – AKB Oct 22 '14 at 19:54
  • I understand that. But that doesn't answer my question. How is the Spring dispatcher servlet mapped? What happens when typing the address http://your.host/MyProject-UX/images/success.gif in the address bar? – JB Nizet Oct 22 '14 at 19:59
  • @JBNizet, it gives 404 error, when I verify in my war file, image is located at images/success.gif. – AKB Oct 22 '14 at 20:17

1 Answers1

0

To my spring config file(dispatcher-servlet.xml or applicationContext.xml)I have added

<mvc:default-servlet-handler />

It worked. Container's default servlet for static resources handling.

or use:

<mvc:resources mapping="/resources/**" location="/resources/" />

create images folder under src/main/webapp/resources/images/allimage.gif

Spring Link: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-default-servlet-handler

AKB
  • 5,918
  • 10
  • 53
  • 90