Reputation: 91
I am trying to return a JSP from a Spring-MVC controller. While everything else is working fine; images and jquery is not getting loaded properly. Below is my configuration.
web.xml
*************
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.0">
<display-name>Real-App</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
dispatcher-servlet.xml is as below:
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
In my JSP, I am loading images using the below code.
Images:
<img src="<c:url value="/resources/images/SomeImage.png" />" alt="" />
jQuery:
<c:url var="jq" value="/resources/scripts/jquery-1.10.2.js" />
<script type="text/javascript" src="${jq}"></script>
Both are not working and I am seeing the below messages in the Chrome debugger.
GET http://localhost:8080/Real-App/resources/images/SomeImage.png 404 (Not Found)
GET http://localhost:8080/Real-App/resources/scripts/jquery-1.10.2.js
Any ideas what went wrong?
Best Regards, Mouli.
Upvotes: 0
Views: 1565
Reputation: 8322
As your dispatcher url-pattern is /, spring will look for @RequestMapping
for all the URL's, you need to use the mvc:resources
tag for excluding from this
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/"/>
Upvotes: 2