Java “我该怎么做?”;加上;一个胸腺细胞片段,而不是替换
我希望能够获取一个thymeleaf片段,使用其中的所有内容,但是有时候,从使用它的页面添加额外的内容。典型情况:我有一个页脚模板,其中包含一个脚本引用,我希望在每一页上。在某些页面上,我想添加另一个脚本引用,但不是全部 在我的web应用程序中,我有以下设置(降低了复杂性以专注于这一点): layout.html:Java “我该怎么做?”;加上;一个胸腺细胞片段,而不是替换,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我希望能够获取一个thymeleaf片段,使用其中的所有内容,但是有时候,从使用它的页面添加额外的内容。典型情况:我有一个页脚模板,其中包含一个脚本引用,我希望在每一页上。在某些页面上,我想添加另一个脚本引用,但不是全部 在我的web应用程序中,我有以下设置(降低了复杂性以专注于这一点): layout.html: <html> <body> <div class="container"> <div fra
<html>
<body>
<div class="container">
<div fragment="content"></div>
</div>
<div th:replace="shared/footer :: footer"></div>
</body>
</html>
共享/footer.html
<html>
<body>
<div th:fragment="footer" th:remove="tag">
<script th:src="{@/scripts/a.js}"></script>
</div>
</body>
</html>
index.html
<html>
<body>
<div th:fragment="content" class="myClass">
<h1>Oh hello</h1>
</div>
<!-- what do i put here?!!! -->
</body>
</html>
哦,你好
我想做的是能够从index.html向页脚添加一些额外的内容:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
Hello World
</body>
<!-- Calling default scripts + custom scripts -->
<div th:replace="fragments/script :: base_script(~{::script})">
<script th:src="@{js/customjs1.min.js}"></script>
<script th:src="@{js/customjs2.min.js}"></script>
<script th:src="@{js/customjs3.min.js}"></script>
</div>
<!-- If you want to call only default scripts -->
<!--<head th:replace="fragments/script :: base_script(null)"></head>-->
</html>
e、 g:
最后的结果是这样的:
<html>
<body>
<div class="container">
<div class="myClass">
<h1>Oh hello</h1>
</div>
</div>
<div>
<script src="/scripts/a.js"></script>
<script src="/scripts/b.js"></script>
</div>
</body>
</html>
哦,你好
显然,th:AddExtrastaff不存在——但你明白了。我想要现有的内容,并且能够提供我自己的内容。我认为,如果我把所有的可能性都放在片段中,然后使用评估来决定是否实际包含这种可能性,这将变得太复杂。不过我可能错了 解决这个问题的一种方法是,在不使用自定义方言的情况下,使用片段上的参数作为布局模板,我称之为“主布局”。本例允许您省略不需要覆盖的元素,但您提供的任何元素都将添加到“主布局”片段中的任何内容 基本模板如下所示 main-view.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:fragment="main-layout">
<head>
<title>Template Head Title</title>
<th:block th:replace="${head} ?: ~{}" />
</head>
<body>
<header>
Common Template Header Here
<th:block th:replace="${contentHeader} ?: ~{}" />
</header>
<main>
Common Template Content Here
<th:block th:replace="${content} ?: ~{}" />
</main>
</body>
<footer>
Common Template Footer Here
<th:block th:replace="${footer} ?: ~{}" />
</footer>
</html>
模板标题
公共模板标题在这里
常见的模板内容在这里
通用模板页脚在这里
使用它看起来像
<!DOCTYPE HTML>
<html th:replace="main-view.html :: main-layout (head=~{:: head}, contentHeader=~{:: header}, content=~{:: main}, footer=~{:: footer})">
<head th:remove="tag">
<title>Greeting Head</title>
</head>
<body>
<header th:remove="tag">
Greeting Content Header
</header>
<main th:remove="tag">
Greeting Content
</main>
</body>
<footer th:remove="tag">
Greeting Footer
</footer>
</html>
迎头
问候语内容标题
问候内容
问候语页脚
我解决这个问题的一种方法是,在我用作布局模板的片段上使用参数,我称之为“主布局”。此示例允许您省去不需要覆盖的元素,但您提供的任何元素都将添加到“主布局”片段中的任何内容。我不确定是否理解您想要做的事情,但这如何。index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
Hello World
</body>
<!-- Calling default scripts + custom scripts -->
<div th:replace="fragments/script :: base_script(~{::script})">
<script th:src="@{js/customjs1.min.js}"></script>
<script th:src="@{js/customjs2.min.js}"></script>
<script th:src="@{js/customjs3.min.js}"></script>
</div>
<!-- If you want to call only default scripts -->
<!--<head th:replace="fragments/script :: base_script(null)"></head>-->
</html>
你好,世界
默认情况下会调用默认js文件,但您可以添加其他脚本。模板/fragments/script.html:
<html xmlns:th="http://www.thymeleaf.org">
<div th:fragment="base_script(scripts)">
<!-- /* default js files */ -->
<script th:src="@{js/defaul1.min.js}"></script>
<script th:src="@{js/defaul2.min.js}"></script>
<script th:src="@{js/defaul3.min.js}"></script>
<th:block th:if="${scripts != null}">
<th:block th:replace="${scripts}" />
</th:block>
</div>
</html>
默认js文件和自定义js文件都将在index.html中调用
<!DOCTYPE html>
<html>
<body>
Hello World
</body>
<!-- Calling default scripts + custom scripts -->
<div>
<!-- /* default js + custom js files */ -->
<script src="/js/defaul1.min.js"></script>
<script src="/js/defaul2.min.js"></script>
<script src="/js/defaul3.min.js"></script>
<script src="/js/customjs1.min.js"></script>
<script src="/js/customjs2.min.js"></script>
<script src="/js/customjs3.min.js"></script>
</div>
</html>
你好,世界
由于无耻地复制粘贴了以前的答案而放弃投票。