【问题标题】:Thymeleaf attributes with no html element没有 html 元素的 Thymeleaf 属性
【发布时间】:2013-05-07 16:59:58
【问题描述】:

我所做的是使用 Thymeleaf 属性,但我不想拥有例如额外的 div。

比如我有这个

<div sec:authorize="hasRole('ROLE_ADMIN')">
    This content is only shown to administrators. 
</div>

但我想要这样的东西:

<sec:authorize="hasRole('ROLE_ADMIN')">
    This content is only shown to administrators. 
</>

这可能吗?

【问题讨论】:

  • 您发布的内容不是有效的 xml/html。 Thymeleaf 期望具有可用于呈现视图的自定义属性的有效 html(标签)。
  • 嗨 Sotirios,我知道这是无效的,这是我需要的一个例子。你知道怎么处理吗?
  • 我想说的是,由于 HTML 没有这样的标签,而 Thymeleaf 使用 HTML 标签来呈现模板,你将无法使用 Thymeleaf 实现你想要的。据我所知没有。

标签: java thymeleaf


【解决方案1】:

你可以使用th:block标签:

<th:block sec:authorize="hasRole('ROLE_ADMIN')">
    This content is only shown to administrators. 
</th:block>

来自 Thymeleaf 文档:

th:block 只是一个属性容器,它允许模板开发人员指定他们想要的任何属性。 Thymeleaf 将执行这些属性,然后简单地使块消失得无影无踪。

【讨论】:

    猜你喜欢
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2014-05-11
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多