📄 basicsyntax.html
字号:
<html><head><META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 3. Syntax</title><link href="../docbook.css" rel="stylesheet" type="text/css"><meta content="DocBook XSL Stylesheets V1.65.0" name="generator"><link rel="home" href="index.html" title="OGNL Language Guide"><link rel="up" href="index.html" title="OGNL Language Guide"><link rel="previous" href="history.html" title="Chapter 2. History"><link rel="next" href="basicExpressions.html" title="Chapter 4. Expressions"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table summary="Navigation header" width="100%"><tr><th align="center" colspan="3">Chapter 3. Syntax</th></tr><tr><td align="left" width="20%"><a accesskey="p" href="history.html"><img src="../images/navigation/prev.gif" alt="Prev"></a> </td><th align="center" width="60%"> </th><td align="right" width="20%"> <a accesskey="n" href="basicExpressions.html"><img src="../images/navigation/next.gif" alt="Next"></a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="basicSyntax"></a>Chapter 3. Syntax</h2></div></div><div></div></div><p>Simple <span class="acronym">OGNL</span> expressions are very simple. The language has become quite rich with features, but you don't generally need to worry about the more complicated parts of the language: the simple cases have remained
that way. For example, to get at the name property of an object, the <span class="acronym">OGNL</span> expression is simply <span class="property">name</span>. To get at the <span class="property">text</span> property of the object returned by the headline property, the
<span class="acronym">OGNL</span> expression is <span class="property">headline.text</span>.</p><p>What is a property? Roughly, an <span class="acronym">OGNL</span> property is the same as a bean property, which means that a pair of get/set methods, or alternatively a field, defines a property (the full story is a bit more complicated, since
properties differ for different kinds of objects; see below for a full explanation).</p><p>The fundamental unit of an <span class="acronym">OGNL</span> expression is the navigation chain, usually just called "chain." The simplest chains consist of the following parts:</p><div class="table"><a name="N100A2"></a><p class="title"><b>Table 3.1. <span class="acronym">OGNL</span> Expression Parts</b></p><table summary="OGNL Expression Parts" border="1"><colgroup><col><col></colgroup><thead><tr><th valign="top">Expression Element Part</th><th valign="top">Example</th></tr></thead><tbody><tr><td valign="top">Property names</td><td valign="top">like the <span class="property">name</span> and <span class="property">headline.text</span> examples above</td></tr><tr><td valign="top">Method Calls</td><td valign="top"><span class="property">hashCode()</span> to return the current object's hash code</td></tr><tr><td valign="top">Array Indices</td><td valign="top"><span class="property">listeners[0]</span> to return the first of the current object's list of listeners</td></tr></tbody></table></div><p>All <span class="acronym">OGNL</span> expressions are evaluated in the context of a current object, and a chain simply uses the result of the previous link in the chain as the current object for the next one. You can extend a chain as long as you
like. For example, this chain:</p><pre class="programlisting">name.toCharArray()[0].numericValue.toString()</pre><p>This expression follows these steps to evaluate:</p><div class="itemizedlist"><ul type="disc"><li><p>extracts the <span class="property">name</span> property of the initial, or root, object (which the user provides to <span class="acronym">OGNL</span> through the <i class="glossterm"><span class="acronym">OGNL</span> context</i>)</p></li><li><p>calls the <span class="property">toCharArray()</span> method on the resulting <tt class="classname">String</tt></p></li><li><p>extracts the first character (the one at index 0) from the resulting array</p></li><li><p>gets the <span class="property">numericValue</span> property from that character (the character is represented as a <tt class="classname">Character</tt> object, and the <tt class="classname">Character</tt> class has a method called
<span class="property">getNumericValue()</span>).</p></li><li><p>calls <span class="property">toString()</span> on the resulting <tt class="classname">Integer</tt> object. The final result of this expression is the <tt class="classname">String</tt> returned by the last <span class="property">toString()</span> call.</p></li></ul></div><p>Note that this example can only be used to get a value from an object, not to set a value. Passing the above expression to the <span class="property">Ognl.setValue()</span> method would cause an <tt class="classname">InappropriateExpressionException</tt>
to be thrown, because the last link in the chain is neither a property name nor an array index.</p><p>This is enough syntax to do the vast majority of what you ever need to do.</p></div><div class="navfooter"><hr><table summary="Navigation footer" width="100%"><tr><td align="left" width="40%"><a accesskey="p" href="history.html"><img src="../images/navigation/prev.gif" alt="Prev"></a> </td><td align="center" width="20%"><a accesskey="u" href="index.html"><img src="../images/navigation/up.gif" alt="Up"></a></td><td align="right" width="40%"> <a accesskey="n" href="basicExpressions.html"><img src="../images/navigation/next.gif" alt="Next"></a></td></tr><tr><td valign="top" align="left" width="40%">Chapter 2. History </td><td align="center" width="20%"><a accesskey="h" href="index.html"><img src="../images/navigation/home.gif" alt="Home"></a></td><td valign="top" align="right" width="40%"> Chapter 4. Expressions</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -