【问题标题】:How to display a message without html tag with Spring and Thymeleaf如何使用 Spring 和 Thymeleaf 显示没有 html 标记的消息
【发布时间】:2018-04-03 08:49:45
【问题描述】:

我有一些 html 代码:

<div class="brand">
    <span class="logo"></span> Title
    <small>subtitle</small>
</div>

在标签中,我可以以这种方式(spring + thymeleaf)显示来自messages.properties(带有语言环境)的文本

<small th:text="#{small.text}" />

但我不知道如何为标题文本显示正确的语言版本。

谢谢你的建议

【问题讨论】:

    标签: html spring thymeleaf message


    【解决方案1】:

    最简单的方法是使用expression inlining。您可以直接在 html 标签中使用表达式,如下所示:

    <h1>
        [[#{h1.text}]]
        <small>[[#{small.text}]]</small>
    </h1>
    

    根据您使用的 thymeleaf 版本,您可能必须使用属性th:inline="text",如下所示:

    <h1 th:inline="text">
        [[#{h1.text}]]
        <small th:inline="text">[[#{small.text}]]</small>
    </h1>
    

    【讨论】:

    • 我解决了这个问题:[[${h1.text}]] 而不是 [[#{h1.text}]]
    【解决方案2】:

    你可以这样做:

    <div class="brand">
        <span class="logo"></span><span th:text="#{title.text}" th:remove="tag"> Title </span>
        <small th:text="#{small.text}">subtitle</small>
    </div>
    

    【讨论】:

    • 如果我们有类似的东西

      Text another small text

      怎么办?
    【解决方案3】:

    您可以将“th:utext”标签(或 data-th-utext)用于“非转义文本”,然后在全局标题文本中添加一些 html 代码和类。 使用您的评论示例,您可以:

    <div class="brand">
        <span class="logo"></span> 
        <h1> 
          <span th:utext=#{title.text}> </span>
        </h1>
    </div>
    

    title.text=Title<br/><small>subtitle</small>
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 2021-06-03
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2014-01-14
      • 1970-01-01
      • 2023-01-04
      相关资源
      最近更新 更多