Javael 表明式 和 ognl表达式

el (expression language)

el 基础操作符

Java 1

el 可以隐含对象(就是可以一直访问的)

Java 2

el 的二种采取情势,第二系列似在jsp中绝非怎么用,首要用于jsf

Java 3

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

Java 4

在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)方法,该格局的概念如下:

Java 5

该办法是抽象类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:

Java,<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.

  1. 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”]

 

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

5.
在逗号后边加上括号,在括号中可以对眼下的靶子值进行引用,例如下边的那么些例子,#this表示listeners.size()的值。 

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

 

  1. 对变量赋值,与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

 

  1. 运用括号改变统计次序以及包装计算单元,方法类似于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” }

 

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

new int[] { 1, 2, 3 }

new int[5]

  1.  定义map

#{ “foo” : “foo value”, “bar” : “bar value” }

  1.  使用ognl遍历Collection类型的靶子,其中的objects表示一个类集对象,
    #this代表遍历的方今因素,然后依照表达式对每种要素举办测算,然后回到总计后得到的list成分,那在ognl官网上称为projecting

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

 

  1.  使用ognl从类集对象中抽取出有些要素,那在ognl官方网站上称之为selection
    ,上边那么些表明式从listeners类集对象中抽取出这一个是ActionListener的因素

listeners.{? #this instanceof ActionListener}

 

利用ognl从类集对象中筛选出首个适合的目的,然后将该对象封装成List重临,原始的做法是listeners.{?
true
}[0],不过那种措施是cumbersome(愚蠢的),因为一旦没有匹配到任何结果或许listeners.{?
true}是空的,那么会抛出ArrayIndexOutOfBoundsException很是。使用下边那种格式会将率先个门当户对的成分作为一个list对象回来,要是没有匹配到此外因素则会回到一个空list。

objects.{^ #this instanceof String }

  1.  相同获取最终一个金童玉女的成分

objects.{$ #this instanceof String }

 

  1. 行使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.

 

  1. 拔取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)

  1.  获取静态属性

 @class@field

 

18.
 表明式的计算,上边那个表明式会将30看成BigInteger放入root对象(应该是value
stack)中

#fact(30H)

 

  1.  伪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。

  1.  Connection 的伪属性

Java 6

  1.  在标点符号上与Java的不等

,(comma)

  逗号用于分开四个单身的表明式,逗号前面的尤其值是该逗号表明式的重返值。比如ensureLoaded(),
name

{}(curly braces)

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

in/not in

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

 

  1.  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中目标的压人顺序:

Java 7

Java 8

相关文章