ValueExpression (Java EE 5)

Java EE API


javax.el Class ValueExpression

java.lang.Object
  extended by javax.el.Expression
      extended by javax.el.ValueExpression
All Implemented Interfaces:
Serializable

public abstract class ValueExpression
extends Expression

Extends: Expression

可以获取或设置值的 Expression

此 API 以前的代号,只能读取表达式。ValueExpression 对象现在可以用于获取值和设置值。可以设置值的表达式称为 l-value 表达式。不能设置值的表达式称为 r-value 表达式。并非所有 r-value 表达式都可以用作 l-value 表达式(例如,"${1+1}""${firstName} ${lastName}")。有关详细信息,请参阅 EL 规范。对不能用作 l-value 的表达式调用 isReadOnly() 必须总是返回 true

可以使用 ExpressionFactory#createValueExpression 方法解析表达式字符串,并返回封装已解析表达式的 ValueExpression 的具体实例。解析时(而不是计算时)使用 FunctionMapper,所以在计算表达式时不需要它。但是,计算时需要 ELContext

每次调用 #getValue#setValue#isReadOnly#getType 方法时将对表达式求值。使用 ELContext 中的 ELResolver 解析顶级变量,并确定 .[] 操作符的行为。对于这 4 个方法中的任何一个,使用 ELResolver#getValue 方法解析除最后一个属性以外的所有属性。这提供 base 对象。最后解析时,ValueExpression 将调用相应的 ELResolver#getValueELResolver#setValueELResolver#isReadOnlyELResolver#getType 方法,具体情况取决于在 ValueExpression 上调用哪个方法。

请参阅 Expression javadoc 中有关比较、序列化和不可变性的注释。

英文文档:

An Expression that can get or set a value.

In previous incarnations of this API, expressions could only be read. ValueExpression objects can now be used both to retrieve a value and to set a value. Expressions that can have a value set on them are referred to as l-value expressions. Those that cannot are referred to as r-value expressions. Not all r-value expressions can be used as l-value expressions (e.g. "${1+1}" or "${firstName} ${lastName}"). See the EL Specification for details. Expressions that cannot be used as l-values must always return true from isReadOnly().

The ExpressionFactory.createValueExpression(javax.el.ELContext, java.lang.String, java.lang.Class) method can be used to parse an expression string and return a concrete instance of ValueExpression that encapsulates the parsed expression. The FunctionMapper is used at parse time, not evaluation time, so one is not needed to evaluate an expression using this class. However, the ELContext is needed at evaluation time.

The getValue(javax.el.ELContext), setValue(javax.el.ELContext, java.lang.Object), isReadOnly(javax.el.ELContext) and getType(javax.el.ELContext) methods will evaluate the expression each time they are called. The ELResolver in the ELContext is used to resolve the top-level variables and to determine the behavior of the . and [] operators. For any of the four methods, the ELResolver.getValue(javax.el.ELContext, java.lang.Object, java.lang.Object) method is used to resolve all properties up to but excluding the last one. This provides the base object. At the last resolution, the ValueExpression will call the corresponding ELResolver.getValue(javax.el.ELContext, java.lang.Object, java.lang.Object), ELResolver.setValue(javax.el.ELContext, java.lang.Object, java.lang.Object, java.lang.Object), ELResolver.isReadOnly(javax.el.ELContext, java.lang.Object, java.lang.Object) or ELResolver.getType(javax.el.ELContext, java.lang.Object, java.lang.Object) method, depending on which was called on the ValueExpression.

See the notes about comparison, serialization and immutability in the Expression javadocs.

Since:
JSP 2.1
See Also:
ELResolver, Expression, ExpressionFactory, Serialized Form

Constructor Summary
 
Method Summary
abstract  Class<?>
abstract  Class<?>
abstract  Object
abstract  boolean
abstract  void
 
Methods inherited from class javax.el.Expression
 
Methods inherited from class java.lang.Object
 

Constructor Detail

public ValueExpression()
英文文档:

ValueExpression

public ValueExpression()
Method Detail

abstract public Object getValue(ELContext context)
相对于提供的上下文对表达式求值,并返回所得值。

所得值会自动强制匹配为创建此表达式时提供给 ExpressionFactorygetExpectedType() 所返回的类型。

context 此计算的上下文。
return 表达式求值的结果。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果由于指定变量或属性不存在或不可读导致某个属性解析操作失败。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getValue

public abstract Object getValue(ELContext context)
Evaluates the expression relative to the provided context, and returns the resulting value.

The resulting value is automatically coerced to the type returned by getExpectedType(), which was provided to the ExpressionFactory when this expression was created.

Parameters:
context - The context of this evaluation.
Returns:
The result of the expression evaluation.
Throws:
NullPointerException - if context is null.
PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
ELException - if an exception was thrown while performing property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

abstract public void setValue(ELContext context, Object value)
相对于提供的上下文对表达式求值,并将结果设置为提供的值。
context 此计算的上下文。
value 要设置的新值。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果由于指定变量或属性不存在或不可读导致某个属性解析操作失败。
ThrowsPropertyNotWritableException: 如果由于指定的变量或属性不可写导致最终变量或属性解析失败。
ThrowsELException: 如果尝试设置属性或变量时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

setValue

public abstract void setValue(ELContext context,
                              Object value)
Evaluates the expression relative to the provided context, and sets the result to the provided value.

Parameters:
context - The context of this evaluation.
value - The new value to be set.
Throws:
NullPointerException - if context is null.
PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
PropertyNotWritableException - if the final variable or property resolution failed because the specified variable or property is not writable.
ELException - if an exception was thrown while attempting to set the property or variable. The thrown exception must be included as the cause property of this exception, if available.

abstract public boolean isReadOnly(ELContext context)
相对于提供的上下文对表达式求值,如果调用 #setValue 总是失败,则返回 true
context 此计算的上下文。
return 如果表达式是只读的,则返回 true;否则返回 false
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果由于指定变量或属性不存在或不可读导致某个属性解析操作失败。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。 * @throws NullPointerException 如果 context 为 null
英文文档:

isReadOnly

public abstract boolean isReadOnly(ELContext context)
Evaluates the expression relative to the provided context, and returns true if a call to setValue(javax.el.ELContext, java.lang.Object) will always fail.

Parameters:
context - The context of this evaluation.
Returns:
true if the expression is read-only or false if not.
Throws:
NullPointerException - if context is null.
PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
ELException - if an exception was thrown while performing property or variable resolution. The thrown exception must be included as the cause property of this exception, if available. * @throws NullPointerException if context is null

abstract public Class<T> getType(ELContext context)
相对于提供的上下文对表达式求值,并返回最一般的类型,该类型对于将来调用 #setValue 方法时作为 value 参数传递的对象而言是可接受的。

这并非总是与 getValue().getClass() 相同。例如,如果是引用数组元素的表达式,则 getType 方法将返回数组元素的类型,这可能是当前位于指定的数组元素中实际元素类型的超类。

context 此计算的上下文。
return 最一般可接受的类型;其他情况未定义。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果由于指定变量或属性不存在或不可读导致某个属性解析操作失败。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getType

public abstract Class<?> getType(ELContext context)
Evaluates the expression relative to the provided context, and returns the most general type that is acceptable for an object to be passed as the value parameter in a future call to the setValue(javax.el.ELContext, java.lang.Object) method.

This is not always the same as getValue().getClass(). For example, in the case of an expression that references an array element, the getType method will return the element type of the array, which might be a superclass of the type of the actual element that is currently in the specified array element.

Parameters:
context - The context of this evaluation.
Returns:
the most general acceptable type; otherwise undefined.
Throws:
NullPointerException - if context is null.
PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
ELException - if an exception was thrown while performing property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

abstract public Class<T> getExpectedType()
返回求值之后将表达式结果强制匹配的类型。
return 传递给创建了此 ValueExpressionExpressionFactory.createValueExpression 方法的 expectedType
英文文档:

getExpectedType

public abstract Class<?> getExpectedType()
Returns the type the result of the expression will be coerced to after evaluation.

Returns:
the expectedType passed to the ExpressionFactory.createValueExpression method that created this ValueExpression.


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!