我是靠谱客的博主 雪白黑猫,这篇文章主要介绍Ognl表达式(根据Apache-Ognl文档直译)1. 什么是Ognl2.Ognl语法3. Expressions(表达式),现在分享给大家,希望可以做个参考。

1. 什么是Ognl

OGNL 代表 Object-Graph Navigation Language;它是一种表达式语言,用于获取和设置 Java 对象的属性,以及其他附加功能,例如列表投影和选择以及 lambda 表达式。您可以使用相同的表达式来获取和设置属性的值。

Ognl 类包含评估 OGNL 表达式的便捷方法。您可以分两个阶段执行此操作,将表达式解析为内部形式,然后使用该内部形式设置或获取属性的值;或者您可以在一个阶段完成,并直接使用表达式的字符串形式获取或设置属性。

OGNL 最初是作为一种使用属性名称在 UI 组件和控制器之间建立关联的方法。随着对更复杂关联的渴望不断增长,Drew Davidson 在 Luke Blanshard 的怂恿下创建了他所谓的 KVCL,即键值编码语言。然后,Luke 使用 ANTLR 重新实现了该语言,提出了新名称,并在 Drew 的怂恿下将其填充到当前状态。后来 Luke 再次使用 JavaCC 重新实现了该语言。 Drew 对所有代码进行了进一步的维护(在 Luke 的精神指导下)。

我们将 OGNL 发音为一个单词,就像“正交”的醉酒发音的最后一个音节。

2.Ognl语法

基本的 OGNL 表达式非常简单。 该语言的功能已经变得相当丰富,但您通常不必担心该语言中更复杂的部分:简单的情况仍然如此。 例如,要获取对象的名称属性,OGNL 表达式就是名称。 要获取headline 属性返回的对象的text 属性,OGNL 表达式是headline.text。

什么是property? 粗略地说,OGNL 属性与 bean 属性相同,这意味着一对 get/set 方法,或者一个字段,定义了一个属性

OGNL 表达式的基本单元是导航链,通常简称为“链”。 最简单的链由以下部分组成:

表达式元素部分例子
属性名称like the name and headline.text examples above
方法调用hashCode() to return the current object’s hash code(返回对象的hashcode)
数组索引listeners[0] to return the first of the current object’s list of listeners (返回第一个)
  • 所有 OGNL 表达式都在当前对象的上下文中进行评估,并且链只是将链中前一个链接的结果用作下一个链接的当前对象。 您可以随意延长链条。 例如,这个链:
name.toCharArray()[0].numericValue.toString()
  • 此表达式按照以下步骤进行评估:
    • extracts the name property of the initial, or root, object(???? 提取初始对象或根对象的名称属性)
    • calls the toCharArray() method on the resulting String(???? 对结果字符串调用 toCharArray() 方法)
    • extracts the first character (the one at index 0) from the resulting array(???? 或者数组中的一个字符[索引为0])
    • gets the numericValue property from that character (the character is represented as a Character object, and the Character class has a method called getNumericValue()); (???? Character对象获取numericValue属性 且且 Character 类有一个名为 getNumericValue()的方法)
    • calls toString() on the resulting Integer object. The final result of this expression is the String returned by the last toString() call.(???? 使用Integer对象调用toString() 此表达式的最终结果是最后一次 toString() 调用返回的字符串。)

请注意,此示例只能用于从对象中获取值,而不能用于设置值。 将上述表达式传递给 Ognl.setValue() 方法会导致抛出 InappropriateExpressionException ,因为链中的最后一个链接既不是属性名称也不是数组索引。

3. Expressions(表达式)

3.1 Constants(常量)

OGNL has the following kinds of constants(常量):

  • String literals, as in Java (with the addition of single quotes): delimited by single- or double-quotes, with the full set of character escapes; ( ???? 单引或者双引号分隔 带转义)
  • Character literals, also as in Java: delimited by single-quotes, also with the full set of escapes; ( ???? 由单引号分隔 带转义)
  • Numeric literals, with a few more kinds than Java. In addition to Java’s ints, longs, floats and doubles, OGNL lets you specify BigDecimals with a “b” or “B” suffix, and BigIntegers with an “h” or “H” suffix (think “huge”—we chose “h” for BigIntegers because it does not interfere with hexadecimal digits);
  • Boolean (true and false) literals;( ???? boolean)
  • The null literal. (???? null )

3.2 Referring to Properties(参考属性)

  • Maps treat all property references as element lookups or storage, with the property name as the key. Lists and arrays treat numeric properties similarly, with the property name as the index, but string properties the same way ordinary objects do. Ordinary objects (that is, all other kinds) only can handle string properties and do so by using “get” and “set” methods (or “is” and “set”)

  • For example, to get the length of an array, you can use this expression( ???? 获取数据长度):

    array.length
    
  • But to get at element 0 of the array, you must use an expression like this( ???? 获取第0个元素):

    array[0]
    

3.3 Indexing(索引)

如上所述,“索引”符号实际上只是属性引用,尽管属性引用的计算形式而不是常量引用。

  • For example, OGNL internally treats the “array.length” expression exactly the same as this expression: ( ???? 该表达式与.length有相同的结果)
    array["length"]
    
  • And this expression would have the same result (though not the same internal form): (???? 表现形式不同 但结果仍相同)
    array["len" + "gth"]
    

3.3.1 Array and List Indexing

For Java arrays and Lists indexing is fairly simple, just like in Java. An integer index is given and that element is the referrent. If the index is out of bounds of the array or List and IndexOutOfBoundsException is thrown, just as in Java.(???? 与Java一样 超出范围也会抛出索引越界异常 )

3.3.2 JavaBeans Indexed Properties

JavaBeans supports the concept of Indexed properties. Specifically this means that an object has a set of methods that follow the following pattern ( ???? Bean支持索引属性 也就是说其都包含以下方法)

  • public PropertyType[] getPropertyName();
  • public void setPropertyName(PropertyType[] anArray);
  • public PropertyType getPropertyName(int index);
  • public void setPropertyName(int index, PropertyType value);

OGNL can interpret this and provide seamless access to the property through the indexing notation. References such as ( ???? 通过索引表示法提供对属性的无缝访问)

someProperty[2]

3.3.3 OGNL Object Indexed Properties

OGNL extends the concept of indexed properties to include indexing with arbitrary objects, not just integers as with JavaBeans Indexed Properties. When finding properties as candidates for object indexing, OGNL looks for patterns of methods with the following signature: ( ???? 扩展索引属性概念,任意对象的索引 而不仅仅是JavaBeans)

  • public PropertyType getPropertyName(IndexType index);
  • public void setPropertyName(IndexType index, PropertyType value);

The PropertyType and IndexType must match each other in the corresponding set and get methods. An actual example of using Object Indexed Properties is with the Servlet API: the Session object has two methods for getting and setting arbitrary attributes: ( ???? PropertyType 和 IndexType 的get 和 set 必须匹配 Session对象中有两种获取或者是设置 任意属性的方法)

public Object getAttribute(String name);
public void setAttribute(String name, Object value);

最后

以上就是雪白黑猫最近收集整理的关于Ognl表达式(根据Apache-Ognl文档直译)1. 什么是Ognl2.Ognl语法3. Expressions(表达式)的全部内容,更多相关Ognl表达式(根据Apache-Ognl文档直译)1.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部