Thymeleaf:我可以在表达式内使用消息吗 [英] Thymeleaf: Can I use messages inside expressions

查看:144
本文介绍了Thymeleaf:我可以在表达式内使用消息吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Thymeleaf 3 within a Spring Boot application. Currently I am in a situation where I want to use a message expression inside an EL expression (Spring EL).

第一个用例:修剪消息

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

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

Second use case: conditionally create an attribute with a message as its value

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

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

Both examples will produce a syntax error, because #{ is not an allowed start of an expression.

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

Any ideas how to achieve what I want?

推荐答案

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

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

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

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

这篇关于Thymeleaf:我可以在表达式内使用消息吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆