0

I am using Spring MVC 3.0.

I have to load the images which exist in the folder "images" parallel to the WEB-INFdirectory.

I have the jsp files in WEB-INF/jsp folder.

The folder structure is:

-app --images --WEB-INF ---jsp ---classes ... 

In web.xml the url mapping for DispatcherServlet is some thing like

<servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

Now in JSP if I load the jsp in the following way:

<img src="<%=request.getContextPath()%>/images/calogo.jpg" /> 

Its now working as DispatcherServlet is intercepting it I guess.

1

2 Answers 2

1

Yes you are right. Your dispatcher is intercepting your request to display the images.

According to me when you try to access the image in your jsp file it will give you 404 error.

You need to include the following line of code in your servlet.xml file.

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

And then everything will work.

Hope this helps you.

Cheers.

Sign up to request clarification or add additional context in comments.

5 Comments

Yaah ... now I am able to access the images but the actual requests are not getting intercepted.I am getting a 404 for normal requests.
Then check your request mapping. Or show what error you are getting in your console.
I added the below lines to my xml and it started working . <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>. Can some body explain what this exactly means.
It will support @RequestParam binding and @ModelAttribute binding in the controller methods. Please upvote my answer if it really helped you.
Ya I have seen that after I put the coment. Thanks. :)
0

If you map / to DispatcherServlet, be sure to enable default servlet handler in Spring's config:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!-- add this to make Spring properly handle resources (e.g., images) --> <mvc:default-servlet-handler /> 

Also, do not use <%... syntax use JSTL:

<img src="<c:url value="/images/calogo.jpg" />" /> 

(or better - do not use JSP at all, use e.g. ThymeLeaf)

1 Comment

I am getting the following exception <br/>javax.servlet.ServletException: No adapter for handler [public java.lang.String com.ca.myca.controllers.WelcomeController.getStarted()]: Does your handler implement a supported interface like Controller? org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1077)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.