Spring 4.1配置静态内容

时间:2015-03-02 23:52:14

标签: java spring spring-mvc

我正在尝试修改Mkyong的Spring 3 MVC和JSON示例(http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/)以使用Spring 4.1.x并允许在webapp目录下访问html文件。对于带有<mvc:resources mapping="/*" location="/WEB-INF" />的4.0.x servlet-mapping,诀窍,但它在4.1.x中不起作用; http://localhost:8080/SpringMVCRest/index.html给出了404错误。我该如何配置?

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>SpringMVCRest</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC Json Webapp</name>
<url>http://maven.apache.org</url>

<properties>
    <spring.version>4.1.5.RELEASE</spring.version>
    <jackson.version>1.9.10</jackson.version>
    <jdk.version>1.6</jdk.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.4.3</version>
    </dependency>

</dependencies>

<build>
    <finalName>SpringMVCRest</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
                <wtpversion>2.0</wtpversion>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

MVC-调度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.xsd">

<context:component-scan base-package="com.mkyong.common.controller" />

<mvc:annotation-driven />

<mvc:resources mapping="/*" location="/WEB-INF" /> 

</beans>

的web.xml:

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring Web MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

控制器:

@Controller
@RequestMapping("/")
public class JSONController {
@RequestMapping(value = "/rest/{name}", method = RequestMethod.GET)
public @ResponseBody
Shop getShopInJSON(@PathVariable String name) {
    Shop shop = new Shop();
    shop.setName(name);
    shop.setStaffName(new String[] { "mkyong1", "mkyong2" });
    return shop;
    }   
}

1 个答案:

答案 0 :(得分:1)

如果您的HTML文件位于webapp目录的根目录中,则它们不在WEB-INF下。所以

<mvc:resources mapping="/*" location="/WEB-INF" />

应该是

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