Thymeleaf:我可以在表达式内使用消息吗

obe*_*ker 2 spring el thymeleaf

我在Spring Boot应用程序中使用Thymeleaf 3。目前,我要在EL表达式(Spring EL)中使用消息表达式。

第一个用例:修剪消息

data:title="${#{message.key}.trim()}
Run Code Online (Sandbox Code Playgroud)

第二个用例:有条件地创建一个属性,并以消息作为其值

data:title="${condition ? #{message.key} : ''}
Run Code Online (Sandbox Code Playgroud)

这两个示例都将产生语法错误,因为#{不允许使用表达式的开头。

有什么想法可以实现我想要的吗?

Met*_*ids 5

在这两种情况下,您都需要使用#messages实用程序对象。

data:title="${#messages.msg('key').trim()}"

data:title="${condition ? #messages.msg('key') : ''}"
Run Code Online (Sandbox Code Playgroud)