【问题标题】:Can thymeleaf print html code?thymeleaf 可以打印html代码吗?
【发布时间】:2017-03-11 14:34:23
【问题描述】:

我有一个来自 SpringMVC 模型的对象“项目”。

如果“item”等于1,我要打印:

<input .../>

否则,我想打印:

<span.../>

我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: spring-mvc thymeleaf


    【解决方案1】:

    我会用 switch 语句来做到这一点......

    <th:block th:switch="${item}">
      <input th:case="1" />
      <span th:case="*" />
    </div>
    

    或多个 if/unless

    <input th:if="${item == 1}" />
    <span th:unless="${item == 1}" />
    

    你可以用th:utext直接把它放到html中,但我不认为这是一个很好的主意(因为你不能在生成的html中使用th:statments,你也不能验证为文档等等……)。

    <th:block th:utext="${item == 1 ? '<input .../>' : '<span.../>'}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2021-06-22
      • 2016-12-08
      • 2022-12-06
      • 2011-08-01
      • 2016-02-09
      相关资源
      最近更新 更多