Issue
I heard the term 'thymeleaf' as view engine. But I don't know where it's working in web application please clarify this.
Solution
They are more commonly called template engines, and provide tools to display the data contained in your Java objects into dinamically generated HTML.
A quick example so you understand what that means, you have a @Controller
with this one mapping:
@GetMapping("/example")
public String getExample(Model model) {
List<ExampleDTO> list = exampleService.findAll();
model.addAttribute("list", list);
return "list-all";
}
ExampleDTO.java
has two string attributes "name" and "label". And you have an HTML template (list-all.html
) like this:
...
<div th:each="item : ${list}">
Name: <span th:text="${item.name}"></span><br>
Label: <span th:text="${item.label}"></span>
</div>
...
Here th:each
and th:text
are both evaluated by Thymeleaf and replaced with HTML. th:each
will iterate through a collection and generate as many elements as collection.size()
while th:text
simply replaces the content of the tag with the attribute value of the Java object.
In the end the browser will receive an HTML like the following (imagine the list contained 3 items):
...
<div>
Name: <span>Item 1</span><br>
Label: <span>IT1</span>
</div>
<div>
Name: <span>Item 2</span><br>
Label: <span>IT2</span>
</div>
<div>
Name: <span>Item 3</span><br>
Label: <span>IT3</span>
</div>
...
Thymeleaf is very powerful, more info here.
Answered By - vicpermir
Answer Checked By - Terry (JavaFixing Volunteer)