Spring不提供静态内容

时间:2013-11-02 19:27:18

标签: java spring spring-mvc

我正在尝试使用Spring mvc:resource提供静态内容。问题是静态内容没有加载。当我检查我的请求时,我意识到HTTP 400 BAD REQUEST响应是从资源返回的(在我的情况和图像中)。

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
 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>'/>

项目结构

File Structure

我已经看过其他问题了,似乎没有一个能解决我的问题。是什么导致了这个问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

当您使用

时,Spring会注册ResourceHttpRequestHandler来处理资源
<mvc:resources mapping="/resources/**" location="/resources/"/>

此处理程序无法返回400 Bad Request响应。您必须将@Controller与映射处理程序匹配到/resources[..].的匹配项。 Spring将在资源处理程序之前检查@Controller处理程序。