我尝试启动在Spring Initializr中创建的演示应用程序。 Spring Boot 版本 - 2.0.3。
我创建了index.html并将其放入resources/templates
文件夹。当我启动spring-boot:run
时,控制台中的消息之一表示:
Tomcat started on port(s): 8080 (http) with context path ''
当我打开localhost:8080
时,它给了我 Spring 的 Whitelabel 错误页面
我尝试添加行 server.servlet.context-path=/
在resources/application.properties
文件。但这没有帮助。
如何设置正确的索引路径?
UPD:似乎问题仅出在模板部分。
index.html
的内容在resources/templates
:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello {{ name }}!!!</h1>
</body>
</html>
IndexController.java
的内容:
@Controller
public class IndexController {
@GetMapping("/")
public ModelAndView index() {
Map<String, String> model = new HashMap<>();
model.put("name", "world");
return new ModelAndView("index", model);
}
我在 pom.xml
中使用 mustache :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
<dependency>
它给了我白标错误。即使有server.servlet.context-path=/
在resources/application.properties
如果我将文件放在 resource/static
中,并将 IndexController
中的文件重命名为“index.html” ,显示出来了,但是“name”显然没有被替换。
我做错了什么?
最佳答案
最后,this answer帮助了。看来这是 mustache 的特征。这很奇怪,因为在 SpringBoot 1.5.x 中,空文件 application.properties
一切正常。如果有人知道 SpringBoot 2.0 为何发生变化 - 请在评论中分享。
关于java - Spring Boot v2.0看不到模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51039387/