如何使用Spring和Thymeleaf显示不带HTML标签的消息

4

我有一些HTML代码:

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

在标签中,我可以以这种方式显示来自messages.properties(使用区域设置)的文本(spring + thymeleaf)

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

但是我不知道如何显示适合标题文本的语言版本。

谢谢您的建议。

3个回答

5
最简单的方法是使用表达式内联。您可以直接在html标签中使用表达式,例如:expression inlining
<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}]]。 - Eric

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>

{btsdaf} - Inweo

1
您可以使用“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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接