2015-03-02 91 views
1

我試圖修改Mkyong的Spring 3 MVC和JSON示例(http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/)以使用Spring 4.1.x並允許在webapp目錄下訪問html文件。對於4.0.x <mvc:resources mapping="/*" location="/WEB-INF" />servlet-mapping做了竅門,但它在4.1.x中不起作用; http://localhost:8080/SpringMVCRest/index.html給404錯誤。我如何配置它?靜態內容的Spring 4.1配置

的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

如果你的HTML文件在Web應用程序的根目錄下,他們是不是在WEB-INF。所以

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

應該

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