【问题标题】:Thymeleaf code in Spring Boot attribute modelSpring Boot 属性模型中的 Thymeleaf 代码
【发布时间】:2018-12-16 10:40:34
【问题描述】:

是否可以通过attributeModel向View发送thymeleaf代码,像标准代码一样对待?

我想仅在需要时将一段代码(百里香片段)加载到不同的地方。 但是当我尝试这个时:

Spring Boot 控制器:

model.addAttribute("fragment", "<th:block th:include=\"fragments/header :: body\"></th:block>");

查看:

<div th:text="${fragment}"></div>

在 WebBrowser 作为 TEXT 我有:

<th:block th:include="fragments/header :: body"></th:block> 

我可以强制重新渲染吗?我该怎么做?但是如果不可能的话,我能做些什么来代替呢?

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    th:text 解析的所有内容都设置为 innerHTML 并且不会重新渲染。 您可以在controller 中设置参数,然后检查Thymeleaf(如果设置),然后包含或替换您的片段。像这样:

    控制器

    model.addAttribute("isFragmentBodyShow", true);
    

    查看

    <th:block th:if="${isFragmentBodyShow}" th:include="fragments/header :: body"></th:block> 
    

    您还可以将片段名称从contoller 发送到Thymeleaf,并在th:include 中使用它来包含动态模板。

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 2017-10-01
      • 2013-08-27
      • 2016-11-29
      • 2018-07-18
      • 2019-03-29
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多