我有一个简单的
HomeController.class
:
package com.example.tacos;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "home";
}
}
我还有一个名为
home.html
的模板
<!DOCTYPE HTML>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Hey</p>
</body>
</html>
无论如何,我在浏览器中收到 404,并且 IDE 告诉我无法解析 MVC“视图”,如您在屏幕上看到的那样
嗨,我遇到了同样的问题,我通过删除 Thymeleaf 的版本来修复它,这样 pom 文件就会像这样:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
问题是在 pom.xml 中我有这个:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
而不是这个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
刚开始学习spring,还没有得到足够好的依赖:)
我在项目中遇到了同样的问题:
"Cannot resolve MVC view"
其原因是
misspelling
与文件夹的命名。
结构是:
| |-- resources
| | |-- template
| | | |-- customer.html
| | | |-- customerForm.html
| | | |-- customers.html
| | | |-- index.html
我需要使用该名称作为文件夹的
templates
,但我有 template
。
也许,这对某人也有帮助。
我发现我当前的项目是在另一个以前的项目中创建的。
更改项目位置后修复了错误。
我在我的 pom.xml 中找到了 gigira 建议的正确条目,但仍然收到错误。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
我必须修复 在 IntelliJ IDE 中找不到 Maven 插件 并重建。
我将tomcat的版本从10.0.20更改为8.5.78并解决了。
对于任何使用 Gradle 的人,我的 build.gradle 文件中有这个;
implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'
我已经改成
implementation('org.springframework.boot:spring-boot-starter-thymeleaf:3.0.4')
它对我有用。
我之前添加了依赖项
<!-- Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.1.4</version>
</dependency>
即使我重新加载 Maven 项目,它也不起作用。
然后我添加了
<version>3.1.4</version>
再次重新加载,成功了!