0

I am trying to serve static content using Spring mvc:resource. The problem is that the static content is not loading. When I inspect my request I realize that a HTTP 400 BAD REQUEST response is return from resource (in my case and image).

mvc-config.xml:

 <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

</beans>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">

<display-name>App-Name</display-name>

<!--
    - Location of the XML file that defines the root application context.
    - Applied by ContextLoaderListener.
-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/application-config.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!--
    - Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/mvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

Controller.

 @Controller
 public class MyController {

    @RequestMapping(value = {"/","home"}, method = RequestMethod.GET)
    public ModelAndView home ()
    {


        return new ModelAndView("home");
    }

}

home.jsp

 <img src='<c:url value="/resources/img/library.png"></c:url>'/>

Project Structure

File Structure

I have looked on other question and none of them seems to solve my problem. What is causing this problem and how do I fix it?

7
  • If you use the url directly in a browser address bar, do you also get a 400? Commented Nov 2, 2013 at 19:31
  • Yes, that also returns 400 . Commented Nov 2, 2013 at 19:37
  • You must have a controller somewhere that is mapped to /resources. Let's see your controller mappings. Commented Nov 2, 2013 at 20:31
  • 1
    I'm unable to reproduce your problem. Try to debug the DispatcherServlet#doDispatch method to see how it handles the request. Commented Nov 2, 2013 at 21:20
  • 1
    You are forgetting your VideoWebservice controller. I'm trying to find out why, but the @RequestMapping with no value attribute seems to have higher precedence than any other. Get rid of it or change it. Commented Nov 2, 2013 at 21:41

1 Answer 1

1

Spring registers a ResourceHttpRequestHandler for handling resources when you use

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

This handler cannot return a 400 Bad Request response. You must have a @Controller with a mapped handler to something that matches /resources[..].. Spring will check the @Controller handlers before the resource handler.

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

4 Comments

I am going to edit the question and add the code for my controller.
This is weird cause as you can see there isn't any handler mapped to resources in MyController.
@user2054833 Are you sure there is nothing else you aren't showing us? In your Spring config or in your servlet config?
I just added the entire content of my web.xml file and the only thing that I haven't added is my application context xml file as well as the xml configuration for hibernate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.