html - 如何使用 thymeleaf 从一页中删除 html 片段?

标签 html thymeleaf

我有一个包含在布局中的 html 片段。此布局存在于所有页面中。现在我只有一个页面,我想从中排除这个片段。

布局页面:

<div th:replace="~{chat/head.html :: head}"></div>
<div th:replace="~{chat/footer.html :: footer}"></div>

所有页面:包括布局。 最后几页:包括没有的布局:

<div th:replace="~{chat/footer.html :: footer}"></div>

最佳答案

一种解决方案是在布局页面中添加一个标志:

<div th:replace="~{chat/head.html :: head}"></div>
<th:block th:if="${!#bools.isTrue(hideFooter)}">
    <div th:replace="~{chat/footer.html :: footer}"></div>
</th:block>

然后在服务器上:

@GetMapping("/thatOnePageThatDoesntNeedAFooter")
public String getThatOnePage(Model model) {
   model.addAttribute("hideFooter", true);
   return "thatOnePageThatDoesntNeedAFooter";
}

逻辑可能看起来有点奇怪,但这将减少在除此异常(exception)情况之外的所有其他情况下向模型添加变量的需要。

请注意,您需要将 th:if 放在 th:replace block 之外。如果将它们放在同一行,th:replace 将优先于 th:if

关于html - 如何使用 thymeleaf 从一页中删除 html 片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55099009/

相关文章:

javascript - 使用 Javascript 和 HTML 创建幻灯片放映

javascript - 带有 JS HTML 的文件的扩展名

html - 删除 html 元素之间的空格

javascript - jquery加载函数不调用Spring Boot Controller 来替换thymeleaf模板片段

javascript - 如何以编程方式滚动 Bootstrap 下拉菜单

html - 在 iOS Mail 中使用自定义字体设置 HTML 签名?

spring-mvc - 有没有办法让 Spring Thymeleaf 处理字符串模板?

java - 使用包装对象时出现异常

java - spring boot thymeleaf 资源不适用于所有页面

javascript - 使元素移动,当它在屏幕末尾消失时使其再次出现在屏幕开头