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

时间:2018-01-12 17:16:28

标签: spring thymeleaf el

我在Spring Boot应用程序中使用Thymeleaf 3。 目前我处在一种情况,我想在EL表达式(Spring EL)中使用消息表达式。

第一个用例:修剪消息

data:title="${#{message.key}.trim()}

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

data:title="${condition ? #{message.key} : ''}

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

如何实现我想要的任何想法?

1 个答案:

答案 0 :(得分:5)

In both cases you'll want to use the #messages utility object.

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

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