java - Spring不提供静态内容

标签 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 。

 @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

我查看了其他问题,但似乎没有一个能解决我的问题。是什么导致了这个问题以及如何解决它?

最佳答案

Spring 会注册一个 ResourceHttpRequestHandler 来在您使用时处理资源

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

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

关于java - Spring不提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745915/

相关文章:

java - 从另一个 View 更改 View 的尺寸

java - 当返回位图的方法失败时,如何处理?

java - 按字母顺序对列表进行排序,头部有特定值(如果存在)

java - Spring Bean @Autowired 和@Qauilifier 名称在我的情况下不起作用。任何人都可以帮助我提供确切的工作示例

java - Spring Boot Autowiring 空值

json - 找不到可接受的代表

Java - 优化将值作为位写入字节缓冲区

java - spring "Manager"类中的 @Autowired 注解

java - 如何使用 Spock 在 Spring Boot 过滤器中模拟服务或 stub 服务方法?

找不到 Spring Controller