el (expression language)

el 基础操作符

el 能够隐含对象(就是可以直接访问的)

el 的两种使用方式,第二种好像在jsp中没有什么用,主要用于jsf

el能够访问的对象(javaBean , 类集, 枚举 , 隐含对象)

在oracle官方网站上记录下来的一些我认为有用的东西:

1. 
${customer}
The web container evaluates the variable that appears in an expression by looking up its value according to the behavior of PageContext.findAttribute(String), where the String argument is the name of the variable. For example, when evaluating the expression ${customer}, the container will look for customer in the page, request, session, and application scopes and will return its value. If customer is not found, a null value is returned.

web容器在处理这段代码是后台使用的是 PageContext.findAttribute(String)方法,该方法的定义如下:

该方法是抽象类PageContext继承抽象类JspContext的。

这个方法的定义和上面红色字体的描述基本相同,就是说会按照page -> request -> session -> application 的这个顺序搜寻customer属性,如果有返回,否者返回null。

2.

Where Value Expressions Can Be Used
Value expressions using the ${} delimiters can be used in

Static text

Any standard or custom tag attribute that can accept an expression

The value of an expression in static text is computed and inserted into the current output. Here is an example of an expression embedded in static text:

<some:tag>
some text ${expr} some text
</some:tag>
If the static text appears in a tag body, note that an expression will not be evaluated if the body is declared to be tagdependent.

Lvalue expressions can be used only in tag attributes that can accept lvalue expressions.

A tag attribute value using either an rvalue or lvalue expression can be set in the following ways:

With a single expression construct:

<some:tag value="${expr}"/>
<another:tag value="#{expr}"/>
These expressions are evaluated, and the result is converted to the attribute’s expected type.

With one or more expressions separated or surrounded by text:

<some:tag value="some${expr}${expr}text${expr}"/>
<another:tag value="some#{expr}#{expr}text#{expr}"/>
These kinds of expression, called composite expressions, are evaluated from left to right. Each expression embedded in the composite expression is converted to a String and then concatenated with any intervening text. The resulting String is then converted to the attribute’s expected type.

只看有颜色的字, 第一个说el可以再普通文本和作为标签的值使用,第二个和第三个颜色字分别为在普通文本和作为标签属性的实例。第四个表示el可以和文字任何搭配使用,其中的#我还不知道在jsp中有什么用。

ognl

1. 主要讲了ognl的调用链,该调用链只能用于get值,不能set值。

The fundamental unit of an OGNL expression is the navigation chain, usually just called "chain."

name.toCharArray()[0].numericValue.toString()

Note that this example can only be used to get a value

2. ognl不能使用setValue()这样会导致InappropriateExpressionException 异常抛出

from an object, not to set a value.Passing the above expression to the Ognl.setValue() method would cause an InappropriateExpressionException to be thrown, because the last link in the chain is neither a property name nor an array index.

3. ognl 的几种属性调用方式

For example, OGNL internally treats the "array.length" expression exactly the same as this expression: array["length"] And this expression would have the same result (though not the same internal form):  array["len" + "gth"]

4. ognl同时可以调用方法,当然前提是该方法在ognl中,方法参数用逗号隔开。

5. 在逗号后面加上括号,在括号中可以对前面的对象值进行引用,例如下面的这个例子,#this表示listeners.size()的值。

listeners.size().(#this > 100? 2*#this : 20+#this)

5. 对变量赋值,与Java类似,如下例,#应该和不同范围的变量有关

To assign a value to a variable explicitly, simply write an assignment statement with a variable reference on the left-hand side:

#var = 99

6. 使用括号改变计算次序以及封装计算单元,方法类似于Java

Parenthetical Expressions As you would expect, an expression enclosed in parentheses is evaluated as a unit, separately from any surrounding operators. This can be used to force an evaluation order different from the one that would be implied by OGNL operator precedences. It is also the only way to use the comma operator in a method argument.

7. 在逗号后面使用括号,下面的这段代码会先搜寻headline.parent属性,然后执行ensureLoaded()方法,然后返回parent属性的name属性值

headline.parent.(ensureLoaded(), name)

traverses through the headline and parent properties,ensures that the parent is loaded and then returns (or sets) the parent's name.

8. 直接使用逗号,不使用括号的计算次序,下面这段代码和上面的类似,不过估计使用了默认的对象作为上下文

Top-level expressions can also be chained in this way.The result of the expression is the right-most expression element.

ensureLoaded(), name

This will call ensureLoaded() on the root object, then get the name property of the root object as the result of the expression.

9. 使用花括号定义list对象,下面这个表达式是用于判断name是不是等于null或者Untitiled,我猜测应该是先将name封装成list

name in { null,"Untitled" }

10.  ognl定义原始类型的数组

new int[] { 1, 2, 3 }

new int[5]

11.  定义map

#{ "foo" : "foo value", "bar" : "bar value" }

12.  使用ognl遍历Collection类型的对象,其中的objects表示一个类集对象, #this表示遍历的当前元素,然后按照表达式对每一个元素进行计算,然后返回计算后得到的list元素,这在ognl官网上叫做projecting

objects.{ #this instanceof String ? #this :#this.toString()}

13.  使用ognl从类集对象中抽取出部分元素,这在ognl官方网站上叫做selection ,下面这个表达式从listeners类集对象中抽取出那些是ActionListener的元素

listeners.{? #this instanceof ActionListener}

使用ognl从类集对象中筛选出第一个符合的对象,然后将该对象封装成List返回,原始的做法是listeners.{? true }[0],但是这种方法是cumbersome(笨拙的),因为如果没有匹配到任何结果或者listeners.{? true}是空的,那么会抛出ArrayIndexOutOfBoundsException异常。使用下面这种格式会将第一个匹配的元素作为一个list对象返回,如果没有匹配到任何元素则会返回一个空list。

objects.{^ #this instanceof String }

14.  相同获取最后一个匹配的元素

objects.{$ #this instanceof String }

15. 使用ognl调用构造方法必须使用类的全名

You can create new objects as in Java, with the new operator. One difference is that you must specify the fully qualified class name for classes other than those in the java.lang package.

16. 使用ognl调用静态方法,这个leave out class 是什么意思?

If you leave out class, it defaults to java.lang.Math,to make it easier to call min and max methods. If you specify the class, you must give the fully qualified

name.

@class@method(args)

17.  获取静态属性

@class@field

18.  表达式的计算,下面这个表达式会将30作为BigInteger放入root对象(应该是value stack)中

#fact(30H)

19.  伪Lambda表达式(Pseudo-Lambda Expressions)

#fact = :[#this<=1? 1 : #this*#fact(#this-1)], #fact(30H)

The lambda expression is everything inside the brackets. The #this variable holds the argument to the expression, which is initially 30H, and is then one less for each successive call to the expression.

lambda表达式的所有内容都在一对方括号中,#this变量为lambda传递进参数fact的值,这个fact的值在后面进行了初始化为30H。

20.  Connection 的伪属性

21.  在标点符号上与Java的不同

,(comma)

  逗号用于分开两个独立的表达式,逗号后面的那个值是该逗号表达式的返回值。比如ensureLoaded(), name

{}(curly braces)

  用于创建list对象,比如{ null, true, false }

in/not in

  判断值是否属于某个集合,例如name in {null,"Untitled"} || name

22.  ognl能够执行隐式的强制类型转换,具体规则如下:

Interpreting Objects as Booleans

  Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:

  1) If the object is a Boolean, its value is extracted and returned;

  2) If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false;

  3) If the object is a Character, its boolean value is true if and only if its char value is non-zero;

  Otherwise, its boolean value is true if and only if it is non-null.

Interpreting Objects as Numbers

  Numerical operators try to treat their arguments as numbers. The basic primitive-type wrapper classes (Integer, Double, and so on, including Character   and Boolean, which are treated as integers), and the "big" numeric classes from the java.math package (BigInteger and BigDecimal), are recognized as    special numeric types. Given an object of some other class, OGNL tries to parse the object's string value as a number.Numerical operators that take two   arguments use the following algorithm to decide what type the result should be. The type of the actual result may be wider, if the result does not fit   in the given type.

  1)  If both arguments are of the same type, the result will be of the same type if possible;

  2)  If either argument is not of a recognized numeric class, it will be treated as if it was a Double for the rest of this algorithm;

  3)  If both arguments are approximations to real numbers (Float, Double, or BigDecimal), the result will be the wider type;

  4)  If both arguments are integers (Boolean, Byte, Character, Short, Integer, Long, or BigInteger), the result will be the wider type;

    5)  If one argument is a real type and the other an integer type, the result will be the real type if the integer is narrower than "int"; BigDecimal if the       integer is BigInteger; or the wider of the real type and Double otherwise.

Interpreting Objects as Integers

   Operators that work only on integers, like the bit-shifting operators, treat their arguments as numbers, except that BigDecimals and BigIntegers are   operated on as BigIntegers and all other kinds of numbers are operated on as Longs. For the BigInteger case, the result of these operators remains a   BigInteger; for the Long case, the result is expressed as the same type of the arguments, if it fits, or as a Long otherwise.

Interpreting Objects as Collections

  The projection and selection operators (e1.{e2} and e1.{?e2}), and the in operator, all treat one of their arguments as a collection and walk it. This is   done differently depending on the class of the argument:

  1) Java arrays are walked from front to back;

  2) Members of java.util.Collection are walked by walking their iterators;

  3) Members of java.util.Map are walked by walking iterators over their values;

  4) Members of java.util.Iterator and java.util.Enumeration are walked by iterating them;

  5) Members of java.lang.Number are "walked" by returning integers less than the given number starting with zero;

  All other objects are treated as singleton collections containing only themselves.

文章来源 http://commons.apache.org/proper/commons-ognl/language-guide.html

上面讲的都是ognl的一些特性,下面说一说都在哪些地方使用

The normal usage of OGNL is to embed the language inside of other constructs to provide a place for flexible binding of values from one place to another. An example of this is a web application where values need to be bound from a model of some sort to data transfer objects that are operated on by a view. Another example is an XML configuration file wherein values are generated via expressions which are then bound to configured objects.

上面这段话来自于ognl官方网站,主要是说ognl主要用于view(jsp)中,还有就是嵌入XML文件中,比如在web项目中的web.xml 和 strut中的struts.xml

关于ognl的使用

http://blog.csdn.net/li_tengfei/article/details/6098134。// 详细讲解valueStack,以及Ognl语言特性

http://www.blogjava.net/max/archive/2007/04/28/114417.html   // Ognl的应用

这两篇博客讲的很清楚,我补充一个valueStack中对象的压人顺序:

el 表达式 和 ognl表达式的更多相关文章

  1. el表达式跟ognl表达式的区别(转)

    EL表达式: >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application.>>如果在struts环境中,它除了有在上面的 ...

  2. el表达式跟ognl表达式的区别

    :  EL表达式 单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application. 如果在struts环境中,它除了有在上面的四个作用域的取值功能外,还 ...

  3. struts2 ValueStack详解,页面获取值el表达式、ognl表达式

    http://www.blogjava.net/max/archive/2007/04/28/114417.html 我想用的是el表达式! http://www.cnblogs.com/belief ...

  4. struts.xml中可以使用el表达式和ognl表达式

    转自:http://blog.csdn.net/hzc543806053/article/details/7538723 文件上传链接: 1)Servlet 文件上传 ————  点击打开链接 2)S ...

  5. OGNL表达式中的#、%和$

    $是el表达式  % #是ognl表达式.  http://devilkirin.iteye.com/blog/427375 OGNL表达式非常强大-其中#.%.$这三个符号在OGNL表达式中经常出现 ...

  6. Struts2知识点小结(三)--值栈与ognl表达式

    1.问题一 : 什么是值栈 ValueStack        回顾web阶段 数据交互问题?        客户端提交数据  到  服务器端    request接受数据+BeanUtils实体封装 ...

  7. JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总

    一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出.       1,JSTL标签总结: a).JSTL标签有什么用?          JSTL是由JCP(Java Commu ...

  8. Struts标签、Ognl表达式、el表达式、jstl标签库这四者之间的关系和各自作用

    我之前虽然会用,但是一直分不清彼此之间有什么区别,所以查找资料,将它们进行整合区分,加深了解, 一 介绍 1.Struts2的作用   Struts2标签库提供了主题.模板支持,极大地简化了视图页面的 ...

  9. OGNL表达式与EL表达式

    一.OGNL表达式 a)什么是OGNL? OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言, 通过它简单一致的表达式语法.主要功能:    ...

随机推荐

  1. 农资产品送货车上使用 PDA手持机 现场销售开单 然后开单后能直接通过移动网络传回电脑(云服务器)

    客户挑战与需求 目前很多中小型企业或零售商店都会使用各种进销存管理软件以提高管理效率,但是由于这类软件都是安装在PC端,更多的是帮助客户通过进销存的管理分析企业营运账款,并不能实时地反映当前销售.库存 ...

  2. js与jquery异同

    大家都知道jquery是js的一个库,很多东西大多数简写了,让js写起来特别的方便.但是对与学习的人来说,最好是先学会了js再去学jquery会更好.在学得过程中你会发现两者实现的原理是差不多的,但是 ...

  3. BestCoder Round #73 (div.2)

    1001 Rikka with Chess ans = n / 2 + m / 2 1002 Rikka with Graph 题意:n + 1条边,问减去至少一条使剩下的图连通的方案数. 分析:原来 ...

  4. 疯狂java学习笔记之面向对象(八) - static和final

    一.static: 1.static是一个标识符: - 有static修饰的成员表明该成员是属于类的; - 没有static修饰的成员表明该成员是属于实例/对象的. 2.static修饰的成员(Fie ...

  5. 旧版API的TextInputFormat源码分析

    TextInputFormat类 package org.apache.hadoop.mapred; import java.io.*; import org.apache.hadoop.fs.*; ...

  6. 【Oracle】Oracle 11g 64位安装完后,ora-12541错误和ora-12514错误的解决

    问题描述: 干净的windows2008 64位服务器上安装 oracle 11g R2 64bit服务端,安装完后,NetManager中默认的主机名为localhost,可以测试通过.但是无法在别 ...

  7. BZOJ1114 : [POI2008]鲁滨逊逃生Rob

    设船最宽行列的交点为船的重心,那么只要预处理出重心在每个位置是否可行,以及在边界上走出边界所需的最小值之后,进行一遍BFS即可. 枚举每个点$(x,y)$,求出它上下最近的障碍物的距离.考虑重心在第$ ...

  8. BZOJ 3110 k大数查询 & 树套树

    题意: 有n个位置,每个位置可以看做一个集合,现在要求你实现一个数据结构支持以下功能: 1:在a-b的集合中插入一个数 2:询问a-b集合中所有元素的第k大. SOL: 调得火大! 李建说数据结构题能 ...

  9. html5调用手机相机并压缩、上传

    近日刚做的一个功能,要在app里使用内嵌页面进行图像的上传.从功能上看,原生的实现应该是最好的.毕竟页面上所有的东西都隔着一个浏览器,所有的实现都要依赖浏览器提供的接口,不同的浏览器对接口的实现又有差 ...

  10. 应用程序间跳转 (友盟SSO 授权 与系统自带的分享)

    应用程序间跳转的应用场景 使用第三方用户登录,如微信登录,返回用户名和密码 需要用户授权,返回到调用程序,同时返回授权的用户名 应用程序推广,跳转到itunes并显示指定app下载页 第三方支付,跳转 ...