gpt4 book ai didi

java - Spring 4.1 静态内容配置

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:21 24 4
gpt4 key购买 nike

我正在尝试修改 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 错误。我该如何配置它?

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-dispatcher-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 :

@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;
}
}

最佳答案

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

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

应该是

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com