首页 > 解决方案 > Spring Boot v2.0 看不到模板

问题描述

我尝试启动在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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mustache</artifactId>
    </dependency>
    <dependency>

它给了我白标签错误。即使server.servlet.context-path=/resources/application.properties

如果我将文件放入resource/static,并将文件重命名为“index.html” IndexController,则会显示,但“name”显然不会被替换。

我做错了什么?

标签: javaspringspring-boottomcat

解决方案


最后,这个答案有所帮助。似乎它的小胡子功能。这很奇怪,因为在 SpringBoot 1.5.x 中,空文件一切正常application.properties。如果有人知道,为什么它会更改为 SpringBoot 2.0 - 请在评论中分享。


推荐阅读