【问题标题】:Process Thymeleaf attributes multiple times多次处理 Thymeleaf 属性
【发布时间】:2014-05-16 19:31:51
【问题描述】:

我希望能够将具有 Thymeleaf 属性的 HTML 作为字符串存储在数据库中。我已经能够存储没有 Thymeleaf 属性的 HTML:

控制器类中的方法

// this method would actually get String from database first
@ModelAttribute("someCode")
public String populateSomeCode() {
    return "This is <h1>some code</h1>";
}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
...
  <body>
    <div th:utext="${someCode}">HTML will go here</div>
  </body>
</html>

这将产生显示“这是一些代码”的 H1 的预期结果。

我想知道是否可以在字符串中包含 Thymeleaf 属性,然后处理这些属性。如果我将字符串更改为:

"This is <h1 th:text=\"'another String'\">some code</h1>"

我希望结果是一个 H1 说“这是另一个字符串”,但目前它仍然会说“这是一些代码”。有没有办法做到这一点?

【问题讨论】:

    标签: spring-mvc template-engine thymeleaf


    【解决方案1】:

    您是否尝试过使用 Thymeleaf __${expression}__ 预处理器功能?

    像这样:

    "This is __<h1 th:text=\"'another String'\"> some code</h1>__"
    

    【讨论】:

    • 预处理器功能正是我所需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2017-04-12
    相关资源
    最近更新 更多