why Thymeleaf's th:text not working in my Spri

2019-08-20 22:21发布

问题:

I have included Thymeleaf for my Spring project for the first time and want to create a simple project(display one word using th:text). But I get nothing in my html page. Why?

Greeting.java

package com.supermegaproject.Main;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class Greeting
{
    @GetMapping("/")
    public String getMessage(Model model)
    {
        model.addAttribute("name", "John");
        return "mainPage";
    }
}

mainPage.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title</title>
</head>
<body>

<h1>Main Page</h1> // DOES APPEAR

<h1 th:text="${name}"></h1> // DOESN'T APPEAR AT ALL

</body>
</html>

At first I thought it may be because of build.gradle. But after checking it looks ok, thymeleaf included, so I don't know why then.

compile('org.springframework.boot:spring-boot-starter-thymeleaf')

Thank you in advance.

回答1:

I solved it. All you had to do - is to delete Mustache dependency.