Browse Source

Improve documentation for literals in SpEL expressions

Closes gh-29700
pull/29710/head
Sam Brannen 3 years ago
parent
commit
57cfb94f1f
  1. 47
      framework-docs/src/docs/asciidoc/core/core-expressions.adoc

47
framework-docs/src/docs/asciidoc/core/core-expressions.adoc

@ -744,25 +744,42 @@ topics:
[[expressions-ref-literal]] [[expressions-ref-literal]]
=== Literal Expressions === Literal Expressions
The types of literal expressions supported are strings, numeric values (int, real, hex), SpEL supports the following types of literal expressions.
boolean, and null. Strings are delimited by single quotation marks. To put a single quotation mark itself
in a string, use two single quotation mark characters.
The following listing shows simple usage of literals. Typically, they are not used - strings
in isolation like this but, rather, as part of a more complex expression -- for example, - numeric values: integer (`int` or `long`), hexadecimal (`int` or `long`), real (`float`
using a literal on one side of a logical comparison operator. or `double`)
- boolean values: `true` or `false`
- null
Strings can delimited by single quotation marks (`'`) or double quotation marks (`"`). To
include a single quotation mark within a string literal enclosed in single quotation
marks, use two adjacent single quotation mark characters. Similarly, to include a double
quotation mark within a string literal enclosed in double quotation marks, use two
adjacent double quotation mark characters.
Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using `Double.parseDouble()`.
The following listing shows simple usage of literals. Typically, they are not used in
isolation like this but, rather, as part of a more complex expression -- for example,
using a literal on one side of a logical comparison operator or as an argument to a
method.
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
---- ----
ExpressionParser parser = new SpelExpressionParser(); ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World" // evaluates to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue(); String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
// evaluates to "Tony's Pizza"
String pizzaParlor = (String) parser.parseExpression("'Tony''s Pizza'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue(); double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
// evals to 2147483647 // evaluates to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue(); int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue(); boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
@ -774,12 +791,15 @@ using a literal on one side of a logical comparison operator.
---- ----
val parser = SpelExpressionParser() val parser = SpelExpressionParser()
// evals to "Hello World" // evaluates to "Hello World"
val helloWorld = parser.parseExpression("'Hello World'").value as String val helloWorld = parser.parseExpression("'Hello World'").value as String
// evaluates to "Tony's Pizza"
val pizzaParlor = parser.parseExpression("'Tony''s Pizza'").value as String
val avogadrosNumber = parser.parseExpression("6.0221415E+23").value as Double val avogadrosNumber = parser.parseExpression("6.0221415E+23").value as Double
// evals to 2147483647 // evaluates to 2147483647
val maxValue = parser.parseExpression("0x7FFFFFFF").value as Int val maxValue = parser.parseExpression("0x7FFFFFFF").value as Int
val trueValue = parser.parseExpression("true").value as Boolean val trueValue = parser.parseExpression("true").value as Boolean
@ -787,9 +807,6 @@ using a literal on one side of a logical comparison operator.
val nullValue = parser.parseExpression("null").value val nullValue = parser.parseExpression("null").value
---- ----
Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using `Double.parseDouble()`.
[[expressions-properties-arrays]] [[expressions-properties-arrays]]
@ -804,7 +821,7 @@ Pupin's city of birth, we use the following expressions:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
---- ----
// evals to 1856 // evaluates to 1856
int year = (Integer) parser.parseExpression("birthdate.year + 1900").getValue(context); int year = (Integer) parser.parseExpression("birthdate.year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.city").getValue(context); String city = (String) parser.parseExpression("placeOfBirth.city").getValue(context);
@ -812,7 +829,7 @@ Pupin's city of birth, we use the following expressions:
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin .Kotlin
---- ----
// evals to 1856 // evaluates to 1856
val year = parser.parseExpression("birthdate.year + 1900").getValue(context) as Int val year = parser.parseExpression("birthdate.year + 1900").getValue(context) as Int
val city = parser.parseExpression("placeOfBirth.city").getValue(context) as String val city = parser.parseExpression("placeOfBirth.city").getValue(context) as String

Loading…
Cancel
Save