简体   繁体   English

Thymeleaf字符串替换和转义

[英]Thymeleaf string substitution and escaping

I have a string which contains raw data, which I want escaped. 我有一个包含原始数据的字符串,我想要转义。 The string also contains markers which I want to replace with span tags. 该字符串还包含我想用span标签替换的标记。

For example my string is 例如我的字符串是

"blah {0}something to span{1} < random chars <" “blah {0}某事要跨越{1} <随机字符<”

I would like the above to be rendered within a div, and replace {0} with and {1} with 我希望上面的内容在div中呈现,并将{0}替换为和{1}

I have tried a number of things, including doing the substitution in my controller, and trying to use the th:utext attribute, however I then get SAX exceptions. 我已经尝试了很多东西,包括在我的控制器中进行替换,并尝试使用th:utext属性,但是然后我得到了SAX异常。

Any ideas? 有任何想法吗?

You can do this using i18n ? 你可以用i18n做到这一点吗?

something like: 就像是:

resource.properties: resource.properties:

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

thymeleaf view: 百里香的观点:

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

The result should be: 结果应该是:

my name is john doe

Im not sure this is a good way. 我不确定这是一个好方法。 But I hope it could help you 但我希望它可以帮到你

It looks using message parameters is the right approach to output formatted strings. 它看起来使用消息参数是输出格式化字符串的正确方法。 See http://www.thymeleaf.org/doc/usingthymeleaf.html#messages http://www.thymeleaf.org/doc/usingthymeleaf.html#messages

I suspect you need to pass character entity reference in order to avoid SAX exceptions 我怀疑你需要传递字符实体引用以避免SAX异常

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

Alternatively place the markup in your .properties file: 或者将标记放在.properties文件中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM