有没有办法将 String.format 传递给 thymeleaf 模板?

Jac*_*icz 4 java spring thymeleaf spring-boot

我正在构建 thymeleaf 模板,我必须向它传递一些带有参数的字符串,例如The value equals %s. 问题是消息可以是任意的,所以我不能只在我的thymeleaf.properties.

我尝试过这样的事情

    <td th:text="#{(${myObject.stringFormatMessage})(${myObject.param})}"></td>
Run Code Online (Sandbox Code Playgroud)

但最终结果是??The value equals %s_eng_ENG??. 我也改了%s但是{0}结果几乎是一样的。在传递到我的模板之前我无法解析该字符串,因为myObject.param它必须是粗体/彩色的。有什么方法可以做到这一点 - 传递 String.format 而不是在 my 中设置外部化文本.properties

lub*_*nac 5

你可以这样使用它:

<td th:text="${T(java.lang.String).format(myObject.stringFormatMessage,myObject.param)}"></td>
Run Code Online (Sandbox Code Playgroud)