简体   繁体   中英

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.

For example my string is

"blah {0}something to span{1} < random chars <"

I would like the above to be rendered within a div, and replace {0} with and {1} with

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.

Any ideas?

You can do this using i18n ?

something like:

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

I suspect you need to pass character entity reference in order to avoid SAX exceptions

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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