Java Thymeleaf有类似JSP标记的东西吗?

Java Thymeleaf有类似JSP标记的东西吗?,java,spring,jsp,spring-mvc,thymeleaf,Java,Spring,Jsp,Spring Mvc,Thymeleaf,我不是说taglibs,我正在使用一个JSP标记来做这样的事情: 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/cu

我不是说taglibs,我正在使用一个JSP标记来做这样的事情:

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>
<%@ 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>
<!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>

这来自子页面

布局.标签

<%@ 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>
<%@ 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>
<!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>

这是来自父级或“布局”

渲染时

<%@ 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>
<%@ 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>
<!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>

这是来自父级或“布局”

这来自子页面

这使我能够在JSP的标题部分包含来自布局和子页面的脚本。正文和页脚也一样

我已经通读了Thymeleaf文档/示例,但可能我没有正确理解,因为看起来我无法完成我试图实现的目标

我之所以“反转”了看似简单的include,是因为我拥有的每个页面都包含某些脚本和标题部分,但我的子页面也包含要导入的脚本和要包含的样式表


我能做到这一点吗?我做错了吗?

默认情况下,Thymeleaf使用所谓的包含样式布局。这种方法在官方网站上的缺点。我建议你使用。这对于创建层次式布局来说更方便


顺便说一下,在布局方言中,
标记的所有内容将自动合并。看一看。

您不想更换
th:replace
?像
@robertotomas一样,我不想替换HTML
节点中的所有内容,我想合并这些内容。我实际上不明白您在做什么。。我想,如果您的示例有多个
jsp:invoke fragments
,则情况会有所不同,但在您提供的示例中,这并不是简单替换所不能做到的。@robertotomás我已经找到了答案,将很快给出答案。谢谢你。您完全可以使用
th:replace
完成此操作。。很高兴您找到了自己的解决方案,我使用了如上所述的Thymeleaf布局方言。我将把我的答案附在我的问题上。