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?
source share