SpringMVC和静态资源

use*_*786 9 java rest spring-mvc tomcat7

我是java和spring的新手.我正在尝试制作你好世界的应用程序而不会得到我做错了什么.

这是我的目录结构:

test_app
-pom.xml
-src
--main
---java
----com.example.web
-----IndexController.java
---webapp
----static
-----img
------example.jpg
----WEB-INF
-----web.xml
-----dispatcher-servlet.xml
-----jsp
------index.jsp
Run Code Online (Sandbox Code Playgroud)

和来源:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         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>Movie Reminder WebApp</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)

调度员servlet.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.example.web"/>
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">

        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

IndexController.java

package com.example.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {
    @RequestMapping(value = "/")
    public ModelAndView index() {
        return new ModelAndView("index");
    }
}
Run Code Online (Sandbox Code Playgroud)

的index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Hello Spring!</title>
</head>
<body>
<img src="<c:url value="/static/img/example.jpg" />" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我部署我的应用程序并请求时,我在图像请求上获得404

http://localhost:8081/  --- http 200 ok 
http://localhost:8081/static/img/example.jpg - http 404 not found
Run Code Online (Sandbox Code Playgroud)

当我将mvc:resources添加到dispatcher-servlet.xml时

<context:annotation-config/>
<context:component-scan base-package="com.example.web"/>
<mvc:resources mapping="/static/**" location="/static/" />
Run Code Online (Sandbox Code Playgroud)

并重新编译我得到404 /请求

http://localhost:8081/  --- http 404 not foundok 
http://localhost:8081/static/img/example.jpg - http 200 ok
Run Code Online (Sandbox Code Playgroud)

请帮我弄清楚我做错了什么

ada*_*shr 6

只需将这两行添加到您的 dispatcher-servlet.xml

<mvc:resources mapping="/static/**" location="/static/" />
<mvc:default-servlet-handler />
Run Code Online (Sandbox Code Playgroud)

以下是文档default-servlet-handler说的内容:

通过转发到Servlet容器的默认Servlet来配置用于提供静态资源的处理程序.使用此处理程序允许使用"/"映射,DispatcherServlet同时仍使用Servlet容器来提供静态资源.此处理程序将所有请求转发到默认Servlet.因此,重要的是它按照所有其他URL HandlerMappings的顺序保持最后.这会是这样的,如果你使用"注释驱动"元素或相反,如果你设置你的自定义HandlerMapping的情况下,一定要其"命令"属性设置为:较低的值DefaultServletHttpRequestHandler,这是Integer.MAX_VALUE.


Jas*_*ues 0

这是因为 servlet 映射。所有传入的请求都被路由到 servlet。但 servlet 不知道如何满足对静态资源的请求。您需要为静态资源添加映射。有几种不同的方法:

  1. 使用Web服务器提供的方式。不幸的是,这会根据您拥有的服务器而有所不同。

  2. 使用可以提供静态资源的 servlet。

您使用什么网络服务器?