This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.2.1! |
Expression Templating
Expression templates allow mixing literal text with one or more evaluation blocks.
Each evaluation block is delimited with prefix and suffix characters that you can
define. A common choice is to use #{ }
as the delimiters, as the following example
shows:
-
Java
-
Kotlin
String randomPhrase = parser.parseExpression(
"random number is #{T(java.lang.Math).random()}",
new TemplateParserContext()).getValue(String.class);
// evaluates to "random number is 0.7038186818312008"
val randomPhrase = parser.parseExpression(
"random number is #{T(java.lang.Math).random()}",
TemplateParserContext()).getValue(String::class.java)
// evaluates to "random number is 0.7038186818312008"
The string is evaluated by concatenating the literal text 'random number is '
with the
result of evaluating the expression inside the { }
delimiters (in this case, the
result of calling that random()
method). The second argument to the parseExpression()
method is of the type ParserContext
. The ParserContext
interface is used to influence
how the expression is parsed in order to support the expression templating functionality.
The TemplateParserContext
used in the previous example resides in the
org.springframework.expression.common
package and is an implementation of the
ParserContext
which by default configures the prefix and suffix to {
and }
,
respectively.