0

我正在尝试使用 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");
    }

}

主页.jsp

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

项目结构

文件结构

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

4

1 回答 1

1

SpringResourceHttpRequestHandler在你使用的时候注册一个来处理资源

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

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

于 2013-11-02T20:33:17.867 回答