Spring-Boot MVC 模板未加载(未找到 404)
Posted
技术标签:
【中文标题】Spring-Boot MVC 模板未加载(未找到 404)【英文标题】:Spring-Boot MVC Template Not Loading (404 Not Found) 【发布时间】:2015-01-03 20:05:11 【问题描述】:我在这里有一个非常简单的 Spring-Boot MVC 应用程序,但它不工作。一个控制器,一个页面未加载并收到 404 未找到。
我在控制器方法中放置了一个 System.out.println("Home Page") 语句,并验证它已正确映射并被正确触发,但模板未加载。
错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).
文件夹结构:
src/main/java
+-Application.java
+-WebController.java
src/main/resources
+-templates
+-index.html
缩写控制台输出:
Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
Mapped "[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String app.WebController.index()
Tomcat started on port(s): 8080/http
Started Application in 5.552 seconds (JVM running for 6.366)
Home Page
Home Page
Home Page
配置:
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter
public static void main(String[] args)
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
控制器:
@Controller
public class WebController
@RequestMapping(value="/")
public String index()
System.out.println("Home Page");
return "index";
index.html:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello</h1>
</body>
</html>
我在这里有一个非常简单的 Spring-Boot MVC 应用程序,但它不工作。一个控制器,一个页面未加载并收到 404 未找到。
我在控制器方法中放置了一个 System.out.println("Home Page") 语句,并验证它已正确映射并被正确触发,但模板未加载。
错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).
文件夹结构:
src/main/java
+-Application.java
+-WebController.java
src/main/resources
+-templates
+-index.html
缩写控制台输出:
Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
Mapped "[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String app.WebController.index()
Tomcat started on port(s): 8080/http
Started Application in 5.552 seconds (JVM running for 6.366)
Home Page
Home Page
Home Page
配置:
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter
public static void main(String[] args)
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
控制器:
@Controller
public class WebController
@RequestMapping(value="/")
public String index()
System.out.println("Home Page");
return "index";
index.html:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello</h1>
</body>
</html>
POM:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.App</groupId>
<artifactId>App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>App</name>
<description>App</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.181</version>
</dependency>
<!--dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency-->
<dependency>
<groupId>com.firebase</groupId>
<artifactId>firebase-client</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.4</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>App.Application</start-class>
<java.version>1.7</java.version>
</properties>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
显示你的 IOC 容器我的意思是你的 xml 文件。 显示你的 web.xml 和 dispatcher-servlet.xml 这是一个 Spring Boot 应用程序,所以我不希望有任何 xml(尤其是 web.xml)。 你在用百里香吗?看起来你正在尝试,但它不在类路径上。 @DaveSyer 是的,我错过了 Thymeleaf!如果找不到“索引”给我一些指示,它不应该抛出错误吗? 【参考方案1】:您的课程路径中似乎没有 Thymeleaf。
添加这个依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后一切都应该正常了。
如果您仍然遇到一些问题,我建议您使用http://start.spring.io/ 创建新项目
【讨论】:
就是这样!如果找不到“索引”给我一些指示,它不应该抛出错误吗?无论如何,谢谢! 我遇到了同样的问题。它至少应该有日志 通过这样做以及在这样做之后为我工作:Maven->BuildProject 我已将其添加到pom.xml
,但仍然没有运气。不知道我错过了什么。
添加后它仍然不适合我。我从一开始就开始了该项目,但仍然没有运气,我认为该项目缺少一些主/父配置。有人遇到这种情况吗?【参考方案2】:
follow this link。给出了很好的例子 http://spring.io/guides/gs/handling-form-submission/
src/main/java/hello/GreetingController.java
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class GreetingController
@GetMapping("/greeting")
public String greetingForm(Model model)
model.addAttribute("greeting", new Greeting());
return "greeting";
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting)
return "result";
src/main/resources/templates/greeting.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@/greeting" th:object="$greeting" method="post">
<p>Id: <input type="text" th:field="*id" /></p>
<p>Message: <input type="text" th:field="*content" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
src/main/resources/templates/result.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + $greeting.id" />
<p th:text="'content: ' + $greeting.content" />
<a href="/greeting">Submit another message</a>
</body>
</html>
src/main/java/hello/Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
public static void main(String[] args)
SpringApplication.run(Application.class, args);
【讨论】:
【参考方案3】:尝试从这个网站http://start.spring.io/创建一个spring boot项目,这是一个spring初始化器,用于创建一个带有spring boot的maven项目。添加 web 和 thymeleaf 依赖并生成项目。将 IDE 中的项目导入为 maven 项目或使用 Spring 工具套件。
DemoApplication.java:
@Controller
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
SpringApplication.run(DemoApplication.class, args);
@RequestMapping(value = "/homepage" , method = RequestMethod.GET )
public String sample()
return "home";
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
home.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>HomePage</title>
</head>
<body>
<h1>MY Spring Boot Home Page</h1>
</body>
</html>
【讨论】:
【参考方案4】:这是一个边缘情况,是偶然的。但是,可能是一个可能的原因。
确保您没有更改application.properties
中的静态资源路径,例如:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/META-INF/resources/webjars/
我将此属性用于 SwaggerUI,它还有其他解决方法。 删除此属性有效。
【讨论】:
以上是关于Spring-Boot MVC 模板未加载(未找到 404)的主要内容,如果未能解决你的问题,请参考以下文章
Spring-boot,无法自动装配类。未找到默认构造函数引发异常
thymeleaf + spring-boot 验证错误未显示在 html 上
在带有 spring-boot rest 和 Swagger 的标头中使用 utf-8 字符时未加载响应
加载资源失败:http://website.azurewebsites.net/signalr/hubs 服务器响应状态为 404(未找到)