簡體   English   中英

Thymeleaf字符串替換和轉義

[英]Thymeleaf string substitution and escaping

我有一個包含原始數據的字符串,我想要轉義。 該字符串還包含我想用span標簽替換的標記。

例如我的字符串是

“blah {0}某事要跨越{1} <隨機字符<”

我希望上面的內容在div中呈現,並將{0}替換為和{1}

我已經嘗試了很多東西,包括在我的控制器中進行替換,並嘗試使用th:utext屬性,但是然后我得到了SAX異常。

有任何想法嗎?

你可以用i18n做到這一點嗎?

就像是:

resource.properties:

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

百里香的觀點:

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

結果應該是:

my name is john doe

我不確定這是一個好方法。 但我希望它可以幫到你

它看起來使用消息參數是輸出格式化字符串的正確方法。 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM