objectprogr
objectprogr

Reputation: 89

Thymeleaf not display value

I tested Thymeleaf and I have a problem, because when I go to localhost and see my html view, dont see my "Test" value, which should be display on the website. This is my code:

@Controller
public class DisplayData {
​
    @RequestMapping("/display")
    public String display(Model model){
        model.addAttribute("now", "Test");
        return "index.html";
    }
}

html view:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta charset=”utf-8″>
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World</h1>
    Now is: <b th:text="${now}"></b>
  </body>
</html>

Path to index.html file is: resources/static/index.html

And localhost view: index.html

Upvotes: 1

Views: 4377

Answers (2)

objectprogr
objectprogr

Reputation: 89

I solved my problem. I changed gradle dependencies: It was: compile group: 'org.thymeleaf', name: 'thymeleaf', version: '3.0.7.RELEASE' Now is: compile("org.springframework.boot:spring-boot-starter-thymeleaf")

Upvotes: 0

Dhaval Simaria
Dhaval Simaria

Reputation: 1964

Three changes are required to make your application up-and-running:

  1. <meta charset=”utf-8″> is not properly quoted. It should be <meta charset="UTF-8"/>
  2. The default location of index.html file should be resources/templates/index.html
  3. Your return statement should be return "index";, since you have annotated your class as @Controller, spring boot will understand that the string returned is the name of the view to be rendered. Then it will automatically check the above-mentioned location to find your view file.

You can refer Spring Boot and Template Engines.

Upvotes: 1

Related Questions