【问题标题】:Thymeleaf - How to display text not included in th:textThymeleaf - 如何显示未包含在 th:text 中的文本
【发布时间】:2021-12-09 04:01:39
【问题描述】:

我想使用 thymeleaf 显示用户名。这是我正在使用的 html 标记:

<h1 th:text="${username}"></h1>

我希望它显示这个:你好,用户

给定的用户名是一个字符串“用户”。我尝试过这样的事情:

<h1 th:text="${username}">Hello, </h1>

但它没有用。我该如何解决?

【问题讨论】:

  • 试试&lt;h1 th:text="|Hello, ${username}|"&gt;&lt;/h1&gt;&lt;h1 th:text="'Hello, ' + ${username}"&gt;&lt;/h1&gt;

标签: java html spring thymeleaf


【解决方案1】:

试试这个:

<h1 th:text="'Hello, ' + ${username}"></h1>

来源: https://www.wimdeblauwe.com/blog/2021/01/11/string-concatenation-with-thymeleaf/

【讨论】:

    【解决方案2】:

    我个人更喜欢只使用一个额外的标签。

    <h1>Hello, <span th:text="${username}" /></h1>
    

    literal substitution

    <h1 th:text="|Hello, ${username}|"></h1>
    

    因为引用字符串只会增加更多复杂性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2021-01-19
      • 2014-09-24
      • 2015-07-18
      • 2016-01-13
      • 2012-04-12
      相关资源
      最近更新 更多