BeanELResolver (Java EE 5)

Java EE API


javax.el Class BeanELResolver

java.lang.Object
  extended by javax.el.ELResolver
      extended by javax.el.BeanELResolver

public class BeanELResolver
extends ELResolver

Extends: ELResolver

使用 JavaBean 组件架构定义对象的属性解析行为。

此解析器处理任何类型的 base 对象,但 base 不能为 null。它接受任何对象作为属性,并将其强制匹配为一个字符串。然后使用该字符串在 base 对象上查找遵从 JavaBean 的属性。使用 JavaBean 获取和设置方法访问该值。

此解析器可以以只读模式构建,这意味着 #isReadOnly 将总是返回 true#setValue 将总是抛出 PropertyNotWritableException

ELResolver 使用 CompositeELResolver 连接到一起,以定义丰富语义来计算表达式。有关 ELResolver 的详细信息,请参阅 javadoc。

由于此解析器处理任何类型的 base 对象,因此应该将其放置在靠近复合解析器末尾的位置。否则,在位于它之后的解析器有机会测试是否能够解析某个属性前,它将声称已经解析了该属性。

英文文档:

Defines property resolution behavior on objects using the JavaBeans component architecture.

This resolver handles base objects of any type, as long as the base is not null. It accepts any object as a property, and coerces it to a string. That string is then used to find a JavaBeans compliant property on the base object. The value is accessed using JavaBeans getters and setters.

This resolver can be constructed in read-only mode, which means that isReadOnly will always return true and setValue(javax.el.ELContext, java.lang.Object, java.lang.Object, java.lang.Object) will always throw PropertyNotWritableException.

ELResolvers are combined together using CompositeELResolvers, to define rich semantics for evaluating an expression. See the javadocs for ELResolver for details.

Because this resolver handles base objects of any type, it should be placed near the end of a composite resolver. Otherwise, it will claim to have resolved a property before any resolvers that come after it get a chance to test if they can do so as well.

Since:
JSP 2.1
See Also:
CompositeELResolver, ELResolver

Nested Class Summary
protected static class
protected static class
 
Field Summary
 
Fields inherited from class javax.el.ELResolver
 
Constructor Summary
 
Method Summary
 Class<?>
 Iterator<FeatureDescriptor>
 Class<?>
 Object
 boolean
 void
 
Methods inherited from class java.lang.Object
 

Constructor Detail

public BeanELResolver()
创建新的读/写 BeanELResolver
英文文档:

BeanELResolver

public BeanELResolver()
Creates a new read/write BeanELResolver.


public BeanELResolver(boolean isReadOnly)
创建新的 BeanELResolver,它的只读状态由给定的参数确定。
isReadOnly 如果此解析器无法修改 Bean,则为 true;否则为 false
英文文档:

BeanELResolver

public BeanELResolver(boolean isReadOnly)
Creates a new BeanELResolver whose read-only status is determined by the given parameter.

Parameters:
isReadOnly - true if this resolver cannot modify beans; false otherwise.

Method Detail

public Class<T> getType(ELContext context, Object base, Object property)
如果 base 对象不为 null,则返回可以在此 Bean 属性上设置的最一般可接受类型。

如果 base 不为 null,则在返回前,此解析器必须将 ELContext 对象的 propertyResolved 属性设置为 true。如果调用此方法后此属性不为 true,则调用者应该忽略返回值。

提供的属性将首先强制匹配为 String。如果此属性有一个 BeanInfoProperty 并且获取它时没有错误,则返回 propertyDescriptorpropertyType。否则将抛出 PropertyNotFoundException

context 此计算的上下文。
base 要分析的 Bean。
property 要分析的属性名称将强制匹配为 String
return 如果 ELContextpropertyResolved 属性设置为 true,则返回最一般的可接受类型;其他情况未定义。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果 base 不为 null,并且指定的属性不存在或不可读。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getType

public Class<?> getType(ELContext context,
                        Object base,
                        Object property)
If the base object is not null, returns the most general acceptable type that can be set on this bean property.

If the base is not null, the propertyResolved property of the ELContext object must be set to true by this resolver, before returning. If this property is not true after this method is called, the caller should ignore the return value.

The provided property will first be coerced to a String. If there is a BeanInfoProperty for this property and there were no errors retrieving it, the propertyType of the propertyDescriptor is returned. Otherwise, a PropertyNotFoundException is thrown.

Specified by:
getType in class ELResolver
Parameters:
context - The context of this evaluation.
base - The bean to analyze.
property - The name of the property to analyze. Will be coerced to a String.
Returns:
If the propertyResolved property of ELContext was set to true, then the most general acceptable type; otherwise undefined.
Throws:
NullPointerException - if context is null
PropertyNotFoundException - if base is not null and the specified property does not exist or is not readable.
ELException - if an exception was thrown while performing the property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

public Object getValue(ELContext context, Object base, Object property)
如果 base 对象不为 null,则返回此 Bean 上给定属性的当前值。

如果 base 不为 null,则在返回前,此解析器必须将 ELContext 对象的 propertyResolved 属性设置为 true。如果调用此方法后此属性不为 true,则调用者应该忽略返回值。

提供的属性名称将首先强制匹配为 String。如果按照 JavaBean 规范,该属性是 base 对象的可读属性,则返回调用获取方法的结果。如果获取方法抛出异常,则该异常将被传播到调用者。如果没有找到该属性或者该属性不可读,则抛出 PropertyNotFoundException

context 此计算的上下文。
base 要获取属性的 Bean。
property 要获取的属性的名称。将强制匹配为 String
return 如果 ELContextpropertyResolved 属性设置为 true,则返回给定属性的值。其他情况未定义。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果 base 不为 null,并且指定的属性不存在或不可读。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getValue

public Object getValue(ELContext context,
                       Object base,
                       Object property)
If the base object is not null, returns the current value of the given property on this bean.

If the base is not null, the propertyResolved property of the ELContext object must be set to true by this resolver, before returning. If this property is not true after this method is called, the caller should ignore the return value.

The provided property name will first be coerced to a String. If the property is a readable property of the base object, as per the JavaBeans specification, then return the result of the getter call. If the getter throws an exception, it is propagated to the caller. If the property is not found or is not readable, a PropertyNotFoundException is thrown.

Specified by:
getValue in class ELResolver
Parameters:
context - The context of this evaluation.
base - The bean on which to get the property.
property - The name of the property to get. Will be coerced to a String.
Returns:
If the propertyResolved property of ELContext was set to true, then the value of the given property. Otherwise, undefined.
Throws:
NullPointerException - if context is null.
PropertyNotFoundException - if base is not null and the specified property does not exist or is not readable.
ELException - if an exception was thrown while performing the property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

public void setValue(ELContext context, Object base, Object property, Object val)
如果 base 对象不为 null,则尝试设置此 Bean 上给定属性的值。

如果 base 不为 null,则在返回前,此解析器必须将 ELContext 对象的 propertyResolved 属性设置为 true。如果调用此方法后此属性不为 true,则调用者可以放心地假定没有设置任何值。

如果此解析器以只读模式构造,则此方法将总是抛出 PropertyNotWritableException

提供的属性名称将首先强制匹配为 String。如果属性是 base(按照 JavaBean 规范)的可写属性,则调用设置方法(传递 value)。如果该属性存在,但没有设置方法,则抛出 PropertyNotFoundException。如果该属性不存在,则抛出 PropertyNotFoundException

context 此计算的上下文。
base 要设置属性的 Bean。
property 要设置的属性的名称。将强制匹配为 String
val 要关联到指定键的值。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果 base 不为 null,并且指定的属性不存在。
ThrowsPropertyNotWritableException: 如果此解析器以只读模式构造,或没有用于该属性的设置方法。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

setValue

public void setValue(ELContext context,
                     Object base,
                     Object property,
                     Object val)
If the base object is not null, attempts to set the value of the given property on this bean.

If the base is not null, the propertyResolved property of the ELContext object must be set to true by this resolver, before returning. If this property is not true after this method is called, the caller can safely assume no value was set.

If this resolver was constructed in read-only mode, this method will always throw PropertyNotWritableException.

The provided property name will first be coerced to a String. If property is a writable property of base (as per the JavaBeans Specification), the setter method is called (passing value). If the property exists but does not have a setter, then a PropertyNotFoundException is thrown. If the property does not exist, a PropertyNotFoundException is thrown.

Specified by:
setValue in class ELResolver
Parameters:
context - The context of this evaluation.
base - The bean on which to set the property.
property - The name of the property to set. Will be coerced to a String.
val - The value to be associated with the specified key.
Throws:
NullPointerException - if context is null.
PropertyNotFoundException - if base is not null and the specified property does not exist.
PropertyNotWritableException - if this resolver was constructed in read-only mode, or if there is no setter for the property.
ELException - if an exception was thrown while performing the property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

public boolean isReadOnly(ELContext context, Object base, Object property)
如果 base 对象不为 null,则返回对 #setValue 的调用是否总是失败。

如果 base 不为 null,则在返回前,此解析器必须将 ELContext 对象的 propertyResolved 属性设置为 true。如果调用此方法后此属性不为 true,则调用者可以放心地假定没有设置任何值。

如果此解析器以只读模式构造,则此方法将总是返回 true

提供的属性名称将首先强制匹配为 String。如果属性为 base 的可写属性,则返回 false。如果找到该属性但该属性不可写,则返回 true。如果找不到该属性,则抛出 PropertyNotFoundException

context 此计算的上下文。
base 要分析的 Bean。
property 要分析的属性名称。将强制匹配为 String
return 如果 ELContextpropertyResolved 属性设置为 true,则调用 setValue 方法总是失败的情况下返回 true,该调用可能成功的情况下返回 false;其他情况未定义。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果 base 不为 null,并且指定的属性不存在。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

isReadOnly

public boolean isReadOnly(ELContext context,
                          Object base,
                          Object property)
If the base object is not null, returns whether a call to setValue(javax.el.ELContext, java.lang.Object, java.lang.Object, java.lang.Object) will always fail.

If the base is not null, the propertyResolved property of the ELContext object must be set to true by this resolver, before returning. If this property is not true after this method is called, the caller can safely assume no value was set.

If this resolver was constructed in read-only mode, this method will always return true.

The provided property name will first be coerced to a String. If property is a writable property of base, false is returned. If the property is found but is not writable, true is returned. If the property is not found, a PropertyNotFoundException is thrown.

Specified by:
isReadOnly in class ELResolver
Parameters:
context - The context of this evaluation.
base - The bean to analyze.
property - The name of the property to analyzed. Will be coerced to a String.
Returns:
If the propertyResolved property of ELContext was set to true, then true if calling the setValue method will always fail or false if it is possible that such a call may succeed; otherwise undefined.
Throws:
NullPointerException - if context is null
PropertyNotFoundException - if base is not null and the specified property does not exist.
ELException - if an exception was thrown while performing the property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

public java.util.Iterator<E> getFeatureDescriptors(ELContext context, Object base)
如果 base 对象不为 null,则返回包含给定对象上可用 JavaBean 属性集的 Iterator。否则返回 null

返回的 Iterator 必须包含 0 个或多个 java.beans.FeatureDescriptor 实例。每个 info 对象包含有关 bean 中某个属性的信息,该信息可以调用 BeanInfo.getPropertyDescriptors 方法获取。使用与 PropertyDescriptor 中的字段相同的字段初始化 FeatureDescriptor,并且需要将指定的属性 "type" 和 "resolvableAtDesignTime" 设置如下:

  • ELResolver#TYPE - 属性的运行时类型,可通过 PropertyDescriptor.getPropertyType() 获取。
  • ELResolver#RESOLVABLE_AT_DESIGN_TIME - true
  • context 此计算的上下文。
    base 要分析的 Bean。
    return 包含 0 个或多个 FeatureDescriptor 对象的 Iterator,每个对象都代表此 Bean 上的一个属性;如果 base 对象为 null,则返回 null
    英文文档:

    getFeatureDescriptors

    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
                                                             Object base)
    If the base object is not null, returns an Iterator containing the set of JavaBeans properties available on the given object. Otherwise, returns null.

    The Iterator returned must contain zero or more instances of FeatureDescriptor. Each info object contains information about a property in the bean, as obtained by calling the BeanInfo.getPropertyDescriptors method. The FeatureDescriptor is initialized using the same fields as are present in the PropertyDescriptor, with the additional required named attributes "type" and "resolvableAtDesignTime" set as follows:

  • ELResolver.TYPE - The runtime type of the property, from PropertyDescriptor.getPropertyType().
  • ELResolver.RESOLVABLE_AT_DESIGN_TIME - true.
  • Specified by:
    getFeatureDescriptors in class ELResolver
    Parameters:
    context - The context of this evaluation.
    base - The bean to analyze.
    Returns:
    An Iterator containing zero or more FeatureDescriptor objects, each representing a property on this bean, or null if the base object is null.
    See Also:
    FeatureDescriptor

    public Class<T> getCommonPropertyType(ELContext context, Object base)
    如果 base 对象不为 null,则返回对于 property 参数此解析器可接受的最一般类型。否则返回 null

    如果 base 对象不为 null,则此方法将总是返回 Object.class。这是因为任何对象都可以作为密钥被接受并且被强制匹配为一个字符串。

    context 此计算的上下文。
    base 要分析的 Bean。
    return 如果 base 为 null,则返回 null;否则返回 Object.class
    英文文档:

    getCommonPropertyType

    public Class<?> getCommonPropertyType(ELContext context,
                                          Object base)
    If the base object is not null, returns the most general type that this resolver accepts for the property argument. Otherwise, returns null.

    Assuming the base is not null, this method will always return Object.class. This is because any object is accepted as a key and is coerced into a string.

    Specified by:
    getCommonPropertyType in class ELResolver
    Parameters:
    context - The context of this evaluation.
    base - The bean to analyze.
    Returns:
    null if base is null; otherwise Object.class.


    Submit a bug or feature

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

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

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