【问题标题】:Thymeleaf string substitution and escapingThymeleaf 字符串替换和转义
【发布时间】:2013-06-07 14:53:30
【问题描述】:

我有一个包含原始数据的字符串,我想对其进行转义。该字符串还包含我想用 span 标签替换的标记。

例如我的字符串是

"blah {0}something to span{1}

我希望在 div 中呈现上述内容,并将 {0} 替换为

我尝试了很多方法,包括在我的控制器中进行替换,并尝试使用 th:utext 属性,但是我得到了 SAX 异常。

有什么想法吗?

【问题讨论】:

    标签: java spring thymeleaf


    【解决方案1】:

    您可以使用 i18n 做到这一点吗?

    类似:

    resource.properties:

    string.pattern=my name is {0} {1}
    

    百里香叶视图:

    <label th:text="#{__${#string.pattern('john', 'doe')}__}"></label>
    

    结果应该是:

    my name is john doe
    

    我不确定这是一个好方法。不过希望对你有帮助

    【讨论】:

      【解决方案2】:

      看起来使用消息参数是输出格式化字符串的正确方法。见http://www.thymeleaf.org/doc/usingthymeleaf.html#messages

      我怀疑您需要传递字符实体引用以避免 SAX 异常

      <span th:utext = "#{string.pattern(${'&lt;span&gt;john&lt;/span&gt;'}, ${'&lt;span&gt;doe&lt;/span&gt;'})}"/>
      

      或者将标记放在您的 .properties 文件中:

      string.pattern=my name is <span>{0}</span> <span>{1}</span>
      

      【讨论】:

        猜你喜欢
        • 2022-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-27
        • 2015-01-17
        • 2012-02-17
        • 2016-03-10
        相关资源
        最近更新 更多