[英]Spring not serving static content
I am trying to serve static content using Spring mvc:resource. 我正在尝试使用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). 当我检查请求时,我意识到HTTP 400 BAD REQUEST响应是从资源(在我的情况和图像中)返回的。
mvc-config.xml: 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 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 针对home.jsp
<img src='<c:url value="/resources/img/library.png"></c:url>'/>
Project 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? 是什么导致此问题,我该如何解决?
Spring registers a ResourceHttpRequestHandler
for handling resources when you use 使用时,Spring注册一个ResourceHttpRequestHandler
来处理资源
<mvc:resources mapping="/resources/**" location="/resources/"/>
This handler cannot return a 400 Bad Request response. 该处理程序无法返回400 Bad Request响应。 You must have a @Controller
with a mapped handler to something that matches /resources[..].
您必须具有一个@Controller
,该@Controller
带有映射的处理程序,该处理程序可以匹配/resources[..].
. 。 Spring will check the @Controller
handlers before the resource handler. Spring将在资源处理程序之前检查@Controller
处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.