Corrected some typos, code samples and changed a bit of wording. All code samples are now SpEL testcases.
This commit is contained in:
parent
9a8bb5f709
commit
f20dcf275f
|
|
@ -25,7 +25,7 @@
|
||||||
<para>While SpEL serves as the foundation for expression evaluation within
|
<para>While SpEL serves as the foundation for expression evaluation within
|
||||||
the Spring portfolio, it is not directly tied to Spring and can be used
|
the Spring portfolio, it is not directly tied to Spring and can be used
|
||||||
independently. In order to be self contained, many of the examples in this
|
independently. In order to be self contained, many of the examples in this
|
||||||
chatper use SpEL as if it was an independent expression language. This
|
chapter use SpEL as if it was an independent expression language. This
|
||||||
requires creating a few boostrapping infrastructure classes such as the
|
requires creating a few boostrapping infrastructure classes such as the
|
||||||
parser. Most Spring users will not need to deal with this infrastructure
|
parser. Most Spring users will not need to deal with this infrastructure
|
||||||
and will instead only author expression strings for evaluation. An example
|
and will instead only author expression strings for evaluation. An example
|
||||||
|
|
@ -136,7 +136,7 @@ String message = (String) exp.getValue();</programlisting>The value of the
|
||||||
can be thrown, <classname>ParseException</classname> and
|
can be thrown, <classname>ParseException</classname> and
|
||||||
<classname>EvaluationException</classname> when calling
|
<classname>EvaluationException</classname> when calling
|
||||||
'<literal>parser.parseExpression</literal>' and
|
'<literal>parser.parseExpression</literal>' and
|
||||||
'<literal>exp.getValue</literal>' respectfully.</para>
|
'<literal>exp.getValue</literal>' respectedly.</para>
|
||||||
|
|
||||||
<para>SpEL supports a wide range of features, such a calling methods,
|
<para>SpEL supports a wide range of features, such a calling methods,
|
||||||
accessing properties and calling constructors. </para>
|
accessing properties and calling constructors. </para>
|
||||||
|
|
@ -145,7 +145,7 @@ String message = (String) exp.getValue();</programlisting>The value of the
|
||||||
the string literal</para>
|
the string literal</para>
|
||||||
|
|
||||||
<programlisting lang="" language="java">ExpressionParser parser = new SpelAntlrExpressionParser();
|
<programlisting lang="" language="java">ExpressionParser parser = new SpelAntlrExpressionParser();
|
||||||
Expression exp = parser.parseExpression("<emphasis role="bold">'Hello World'.concat(!)</emphasis>");
|
Expression exp = parser.parseExpression("<emphasis role="bold">'Hello World'.concat('!')</emphasis>");
|
||||||
String message = (String) exp.getValue();</programlisting>
|
String message = (String) exp.getValue();</programlisting>
|
||||||
|
|
||||||
<para>The value of message is now 'Hello World!'. </para>
|
<para>The value of message is now 'Hello World!'. </para>
|
||||||
|
|
@ -223,9 +223,9 @@ boolean isEqual = exp.getValue(context, Boolean.class); // evaluates to true</p
|
||||||
<title>The EvaluationContext interface </title>
|
<title>The EvaluationContext interface </title>
|
||||||
|
|
||||||
<para>The interface <interfacename>EvaluationContext</interfacename> is
|
<para>The interface <interfacename>EvaluationContext</interfacename> is
|
||||||
used when evaluating an expression to resolve properties, methods
|
used when evaluating an expression to resolve properties, methods,
|
||||||
,fields, and to help perform type conversion. The out-of-the-box
|
fields, and to help perform type conversion. The out-of-the-box
|
||||||
implementation, <classname>StandardEvaluationContext</classname> ,uses
|
implementation, <classname>StandardEvaluationContext</classname>, uses
|
||||||
reflection to manipulate the object, caching
|
reflection to manipulate the object, caching
|
||||||
j<package>ava.lang.reflect</package>'s <classname>Method</classname>,
|
j<package>ava.lang.reflect</package>'s <classname>Method</classname>,
|
||||||
<classname>Field</classname>, and <classname>Constructor</classname>
|
<classname>Field</classname>, and <classname>Constructor</classname>
|
||||||
|
|
@ -502,14 +502,14 @@ boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(
|
||||||
standard operator notation. Support is not yet implemented for objects
|
standard operator notation. Support is not yet implemented for objects
|
||||||
that implement the Comparable interface.</para>
|
that implement the Comparable interface.</para>
|
||||||
|
|
||||||
<para><programlisting language="java">// evaluats to true
|
<para><programlisting language="java">// evaluates to true
|
||||||
boolean isEqual = parser.parseExpression("2 == 2").getValue(Boolean.class);
|
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
|
||||||
|
|
||||||
// evaluates to false
|
// evaluates to false
|
||||||
boolean isEqual = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
|
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
|
||||||
|
|
||||||
// evaluates to true
|
// evaluates to true
|
||||||
boolean isEqual = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);</programlisting>In
|
boolean trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);</programlisting>In
|
||||||
addition to standard relational operators SpEL supports the
|
addition to standard relational operators SpEL supports the
|
||||||
'instanceof' and regular expression based 'matches' operator.</para>
|
'instanceof' and regular expression based 'matches' operator.</para>
|
||||||
|
|
||||||
|
|
@ -542,8 +542,8 @@ boolean trueValue = parser.parseExpression(expression).getValue(societyContext,
|
||||||
|
|
||||||
// -- OR --
|
// -- OR --
|
||||||
|
|
||||||
// evaluates to false
|
// evaluates to true
|
||||||
boolean falseValue = parser.parseExpression("true or false").getValue(Boolean.class);
|
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);
|
||||||
|
|
||||||
// evaluates to true
|
// evaluates to true
|
||||||
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstien')";
|
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstien')";
|
||||||
|
|
@ -596,9 +596,6 @@ int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
|
||||||
|
|
||||||
// Operator precedence
|
// Operator precedence
|
||||||
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21
|
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</programlisting></para>
|
</programlisting></para>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
|
@ -608,8 +605,8 @@ int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class);
|
||||||
|
|
||||||
<para>Setting of a property is done by using the assignment operator.
|
<para>Setting of a property is done by using the assignment operator.
|
||||||
This would typically be done within a call to
|
This would typically be done within a call to
|
||||||
<literal>SetValue</literal> but can also be done inside a call to
|
<literal>setValue</literal> but can also be done inside a call to
|
||||||
<literal>GetValue</literal> </para>
|
<literal>getValue</literal> </para>
|
||||||
|
|
||||||
<programlisting language="java">Inventor inventor = new Inventor();
|
<programlisting language="java">Inventor inventor = new Inventor();
|
||||||
StandardEvaluationContext inventorContext = new StandardEvaluationContext();
|
StandardEvaluationContext inventorContext = new StandardEvaluationContext();
|
||||||
|
|
@ -620,7 +617,6 @@ parser.parseExpression("Name").setValue(inventorContext, "Alexander Seovic2");
|
||||||
// alternatively
|
// alternatively
|
||||||
|
|
||||||
String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inventorContext, String.class);
|
String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inventorContext, String.class);
|
||||||
|
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
||||||
<para></para>
|
<para></para>
|
||||||
|
|
@ -636,7 +632,7 @@ String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inve
|
||||||
<programlisting language="java">Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);
|
<programlisting language="java">Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);
|
||||||
|
|
||||||
|
|
||||||
boolean isEqual = parser.parseExpression("T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR").getValue(Boolean.class);
|
boolean trueValue = parser.parseExpression("T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR").getValue(Boolean.class);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
@ -674,10 +670,10 @@ parser.parseExpression("Name = #newName").getValue(context);
|
||||||
System.out.println(tesla.getName()) // "Mike Tesla"</programlisting>
|
System.out.println(tesla.getName()) // "Mike Tesla"</programlisting>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<title>The #this variables</title>
|
<title>The #this variable</title>
|
||||||
|
|
||||||
<para>The variable #this is always defined and refers to the root
|
<para>The variable #this is always defined and refers to the
|
||||||
object that is currently being evaluated. </para>
|
current evaluation object (the object against which unqualified references will be resolved). </para>
|
||||||
|
|
||||||
<programlisting language="java">// create an array of integers
|
<programlisting language="java">// create an array of integers
|
||||||
List<Integer> primes = new ArrayList<Integer>();
|
List<Integer> primes = new ArrayList<Integer>();
|
||||||
|
|
@ -723,9 +719,6 @@ List<Integer> primesGreaterThanTen = (List<Integer>) parser.parseExp
|
||||||
<para>This method is then registered with the evaluation context and can
|
<para>This method is then registered with the evaluation context and can
|
||||||
be used within an expression string</para>
|
be used within an expression string</para>
|
||||||
|
|
||||||
<para>used in To register this method with the evaluation context and
|
|
||||||
used in an expression string</para>
|
|
||||||
|
|
||||||
<programlisting language="java">ExpressionParser parser = new SpelAntlrExpressionParser();
|
<programlisting language="java">ExpressionParser parser = new SpelAntlrExpressionParser();
|
||||||
StandardEvaluationContext context = new StandardEvaluationContext();
|
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -196,7 +196,7 @@
|
||||||
<title>Spring Expression Language</title>
|
<title>Spring Expression Language</title>
|
||||||
|
|
||||||
<para>Spring introduces an expression language which is similar to Unified
|
<para>Spring introduces an expression language which is similar to Unified
|
||||||
EL in its syntax but offers significantly more feature. The expression
|
EL in its syntax but offers significantly more features. The expression
|
||||||
language can be used when defining XML and Annotation based bean
|
language can be used when defining XML and Annotation based bean
|
||||||
definitions and also serves as the foundation for expression language
|
definitions and also serves as the foundation for expression language
|
||||||
support across the Spring portfolio. Details of this new functionality can
|
support across the Spring portfolio. Details of this new functionality can
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue