1

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

6
  • what happens if you return "index" instead of '"index.html"`. In thymeleaf you normally dont need the file suffix.(in default) Commented Sep 8, 2017 at 7:17
  • nothing display, only error from spring boot (Whitelablel error page). Path to my index.html is resources/static/index.html Commented Sep 8, 2017 at 7:31
  • why is your html in static folder? in default your html should be under templates Commented Sep 8, 2017 at 7:33
  • I dont know, but if i add index.html to templates folder, then again nothing display on localhost only spring boot error page Commented Sep 8, 2017 at 7:37
  • You should start from beginning. Using the defaults and you will have success. Commented Sep 8, 2017 at 15:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

16 Comments

@objectprogr If my answer was helpful in resolving your issue, please do mark it as 'Accepted.
No, after this change I have this error: This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Sep 08 13:37:52 CEST 2017 There was an unexpected error (type=Not Found, status=404). No message available
@objectprogr Hope you have not explicitly configured any other location for your view files other than the default. Because I have tried the exact scenario as yours in my machine and it is working.
I hope that not. This is my whole code on github github.com/objectprogr/fuelApp/tree/fuelV1/src/main. How to configured othr location for view?
@objectprogr Remove @RestController from FuelApplication class. That class is the starter class of your application and not a REST controller.
|
0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.