To avoid copying unnecessary code, I want to divide the page into 3 parts, header, content and footer. Previously, I used jsp and just inserted using <%@ include file = "header.jsp"%> Now I want to learn Thymeleaf and I would like to do something like that. And how to use it in the Spring section. Earlier, I made a controller on the similarity:

 @RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET) public String welcome() { model.addAttribute("Hello", "Hello world"); return "welcome"; } 

Can I leave it like this, or do I need something else for Thymeleaf? I read about the fragments, but did not understand how to connect it with the controller.

    1 answer 1

    In the controller, nothing changes. Just create three files.

    header.html

     <html xmlns:th="http://www.thymeleaf.org"> <body> <div th:fragment="content"> Верхняя часть страницы </div> </body> </html> 

    footer.html

     <html xmlns:th="http://www.thymeleaf.org"> <body> <div th:fragment="content"> Нижняя часть страницы </div> </body> </html> 

    welcome.html

     <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <title>Привет!</title> </head> <body> <div th:replace="header :: content"></div> <div> Основная часть страницы </div> <div th:replace="footer :: content"></div> </body> </html> 

    and everything Thymeleaf has very simple and detailed documentation .