gpt4 book ai didi

java - Spring Boot v2.0 看不到模板

转载 作者:行者123 更新时间:2023-11-30 06:44:10 25 4
gpt4 key购买 nike

我尝试启动在 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 错误页面

我试图在 resources/application.properties 文件中添加 server.servlet.context-path=/ 行。但这没有帮助。

如何设置正确的索引路径?

UPD:似乎问题只出在模板部分。

resources/templatesindex.html 的内容:

<!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>

它给了我白标签错误。即使在 resources/application.properties

中使用 server.servlet.context-path=/

如果我将文件放在 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/

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