Does Thymeleaf have something like JSP tags?

I don't mean taglibs, I use a JSP tag to do something like this:

ChildPage.jsp

<%@ page contentType="text/html" pageEncoding="UTF-8" %> <%@ taglib prefix="t" tagdir="/WEB-INF/tags" %> <t:layout> <jsp:attribute name="head"> <link href="css/custom.css" type="text/css" rel="stylesheet"/> </jsp:attribute> <jsp:attribute name="scripts"> <script src="js/custom.js"></script> </jsp:attribute> <jsp:body> <p>This is from the child page</p> </jsp:body> </t:layout> 

layout.tag

 <%@ tag description="Layout template" pageEncoding="UTF-8" %> <%@ attribute name="head" fragment="true" %> <%@ attribute name="scripts" fragment="true" %> <!DOCTYPE html> <html lang="en"> <head> <link href="css/main.css" type="text/css" rel="stylesheet"/> <jsp:invoke fragment="head"/> </head> <body> <div id="body"> <p>This is from the parent or "layout"</p> <jsp:doBody/> </div> <div id="footer"> <script src="js/main.js"></script> <jsp:invoke fragment="scripts"/> </div> </body> </html> 

When rendering :

 <!DOCTYPE html> <html lang="en"> <head> <link href="css/main.css" type="text/css" rel="stylesheet"/> <link href="css/custom.css" type="text/css" rel="stylesheet"/> </head> <body> <div id="body"> <p>This is from the parent or "layout"</p> <p>This is from the child page</p> </div> <div id="footer"> <script src="js/main.js"></script> <script src="js/custom.js"></script> </div> </body> </html> 

This allows me to include scripts in the JSP header section from both the layout and the child pages. The same goes for the body and footer.

I read Thymeleaf docs or examples, but maybe I donโ€™t get it right, because it doesnโ€™t seem like I can do what I'm trying to achieve.

The reason I โ€œturnedโ€ what seems simple is that every page on which I have certain scripts and a heading section, but my child pages also have scripts that need to be imported, and style sheets to be included .

Can I get it somehow? Am I doing it wrong?

+6
source share
1 answer

By default, Thymeleaf uses the so-called Include-style layouts. The disadvantages of this approach are explained on the official website. I would advise you to use the thimeleaf Dialog Box . This is a much more convenient dialect for creating hierarchical style layouts.

By the way, in the Layout Dialect, the entire contents of the <head> will be merged automatically. Just see an example .

+3
source

All Articles