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

标签 java spring spring-boot tomcat

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

相关文章:

java - 使用 Maven 将 Spring 框架应用程序打包到 war

spring - RestTemplate 所需的 MultipartFile 参数 'file' 不存在

java - 在同一容器中运行多个应用程序时,在 Spring Boot 中外部化配置

spring - 如何在运行时为每个 http 请求选择 bean 实现

java - 更改字体时 JTextField 大小发生变化

java - WebLogic 10.3.7 文件上传返回 "Error creating bean with name ' portletMultipartResolver'"

java - HTMLUnit 认为提交按钮是文本输入

java - Spring MVC 在 Controller 之间传递相同的对象

java - 具有作为构造函数参数传递的通用基类的枚举

java - @TestPropertySource(properties = {"ticketing.profile=test"}) 是否应该自动更新系统属性值?