MapELResolver (Java EE 5)

Java EE API


javax.el Class MapELResolver

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

public class MapELResolver
extends ELResolver

Extends: ELResolver

java.util.Map 的实例上定义属性解析行为。

此解析器处理类型为 java.util.Map 的 base 对象。它接受任何对象作为属性,并且使用该对象作为映射中的键。所得值就是映射中与该键关联的值。

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

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

英文文档:

Defines property resolution behavior on instances of Map.

This resolver handles base objects of type java.util.Map. It accepts any object as a property and uses that object as a key in the map. The resulting value is the value in the map that is associated with that key.

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.

Since:
JSP 2.1
See Also:
CompositeELResolver, ELResolver, Map

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 MapELResolver()
创建新的读/写 MapELResolver
英文文档:

MapELResolver

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


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

MapELResolver

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

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

Method Detail

public Class<T> getType(ELContext context, Object base, Object property)
如果 base 对象是映射,则返回此映射中的值可以接受的最一般类型。

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

假定 base 为 Map,则此方法将总是返回 Object.class。这是因为 Map 接受任何对象作为给定键的值。

context 此计算的上下文。
base 要分析的映射。此解析器仅处理属于 Map 类型的 base。
property 要返回可接受类型的键。此解析器将忽略它。
return 如果 ELContextpropertyResolved 属性设置为 true,则返回最一般的可接受类型;其他情况未定义。
ThrowsNullPointerException: 如果 context 为 null
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getType

public Class<?> getType(ELContext context,
                        Object base,
                        Object property)
If the base object is a map, returns the most general acceptable type for a value in this map.

If the base is a Map, 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.

Assuming the base is a Map, this method will always return Object.class. This is because Maps accept any object as the value for a given key.

Specified by:
getType in class ELResolver
Parameters:
context - The context of this evaluation.
base - The map to analyze. Only bases of type Map are handled by this resolver.
property - The key to return the acceptable type for. Ignored by this resolver.
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
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 对象是映射,则返回与给定键(由 property 参数指定)关联的值。如果未找到该键,则返回 null

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

正如在 java.util.Map#get 中一样,返回 null 并不意味着没有该键的映射;也可能 Map 显式将该键映射为 null

context 此计算的上下文。
base 要分析的映射。此解析器仅处理属于 Map 类型的 base。
property 要返回其关联值的键。
return 如果 ELContextpropertyResolved 属性设置为 true,则返回与给定键关联的值;如果没有找到键,则返回 null。其他情况未定义。
ThrowsClassCastException: 如果该键不是此映射的正确类型(也可由底层 Map 抛出)。
ThrowsNullPointerException: 如果上下文为 null,或者键为 null 并且此映射不允许 null 键(后者也可由底层 Map 抛出)。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getValue

public Object getValue(ELContext context,
                       Object base,
                       Object property)
If the base object is a map, returns the value associated with the given key, as specified by the property argument. If the key was not found, null is returned.

If the base is a Map, 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.

Just as in Map.get(java.lang.Object), just because null is returned doesn't mean there is no mapping for the key; it's also possible that the Map explicitly maps the key to null.

Specified by:
getValue in class ELResolver
Parameters:
context - The context of this evaluation.
base - The map to be analyzed. Only bases of type Map are handled by this resolver.
property - The key whose associated value is to be returned.
Returns:
If the propertyResolved property of ELContext was set to true, then the value associated with the given key or null if the key was not found. Otherwise, undefined.
Throws:
ClassCastException - if the key is of an inappropriate type for this map (optionally thrown by the underlying Map).
NullPointerException - if context is null, or if the key is null and this map does not permit null keys (the latter is optionally thrown by the underlying Map).
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 对象是映射,则尝试设置与给定键(由 property 参数指定)关联的值。

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

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

如果使用 java.util.Collections#unmodifiableMap 创建 Map,则此方法必须抛出 PropertyNotWritableException。不幸的是,没有任何 Collection API 方法可以检测这一点。但是,实现可以使用以下方法解决该问题:创建原型不可修改的 Map,并查询其运行时类型,以确定它是否匹配 base 对象的运行时类型。

context 此计算的上下文。
base 要修改的映射。此解析器仅处理属于 Map 类型的 base。
property 要关联到指定值的键。
val 要关联到指定键的值。
ThrowsClassCastException: 如果指定键或值的类不允许它存储在此映射中。
ThrowsNullPointerException: 如果上下文为 null,或者此映射不允许 null 键或值并且指定的键或值为 null
ThrowsIllegalArgumentException: 如果此键或值的某个方面不允许它存储在此映射中。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
ThrowsPropertyNotWritableException: 如果此解析器以只读模式构造,或者底层映射不支持放置操作。
英文文档:

setValue

public void setValue(ELContext context,
                     Object base,
                     Object property,
                     Object val)
If the base object is a map, attempts to set the value associated with the given key, as specified by the property argument.

If the base is a Map, 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.

If a Map was created using Collections.unmodifiableMap(java.util.Map), this method must throw PropertyNotWritableException. Unfortunately, there is no Collections API method to detect this. However, an implementation can create a prototype unmodifiable Map and query its runtime type to see if it matches the runtime type of the base object as a workaround.

Specified by:
setValue in class ELResolver
Parameters:
context - The context of this evaluation.
base - The map to be modified. Only bases of type Map are handled by this resolver.
property - The key with which the specified value is to be associated.
val - The value to be associated with the specified key.
Throws:
ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
NullPointerException - if context is null, or if this map does not permit null keys or values, and the specified key or value is null.
IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
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.
PropertyNotWritableException - if this resolver was constructed in read-only mode, or if the put operation is not supported by the underlying map.

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

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

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

如果使用 java.util.Collections#unmodifiableMap 创建 Map,则该方法必须返回 true。不幸的是,没有任何 Collection API 方法可以检测这一点。但是,实现可以使用以下方法解决该问题:创建原型不可修改的 Map,并查询其运行时类型,以确定它是否匹配 base 对象的运行时类型。

context 此计算的上下文。
base 要分析的映射。此解析器仅处理属于 Map 类型的 base。
property 要返回只读状态的键。此解析器将忽略它。
return 如果 ELContextpropertyResolved 属性设置为 true,则调用 setValue 方法总是失败的情况下返回 true,该调用可能成功的情况下返回 false;其他情况未定义。
ThrowsNullPointerException: 如果 context 为 null
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

isReadOnly

public boolean isReadOnly(ELContext context,
                          Object base,
                          Object property)
If the base object is a map, 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 a Map, 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.

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

If a Map was created using Collections.unmodifiableMap(java.util.Map), this method must return true. Unfortunately, there is no Collections API method to detect this. However, an implementation can create a prototype unmodifiable Map and query its runtime type to see if it matches the runtime type of the base object as a workaround.

Specified by:
isReadOnly in class ELResolver
Parameters:
context - The context of this evaluation.
base - The map to analyze. Only bases of type Map are handled by this resolver.
property - The key to return the read-only status for. Ignored by this resolver.
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
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 对象是映射,则返回一个 Iterator,它包含 Map 中可用的一组键。否则返回 null

返回的 Iterator 必须包含 0 个或多个 java.beans.FeatureDescriptor 实例。每个 info 对象包含有关 Map 中一个键的信息,并且初始化如下:

  • displayName - 在此键上调用 toString 方法的返回值;如果该键为 null,则为 "null"
  • name – 与 displayName 属性相同。
  • shortDescription – 空字符串
  • expert - false
  • hidden - false
  • preferred - true
  • 此外,必须在返回的 FeatureDescriptor 中设置以下指定的属性:
  • ELResolver#TYPE - 在此键上调用 getClass() 方法的返回值;如果该键为 null,则为 null
  • ELResolver#RESOLVABLE_AT_DESIGN_TIME - true
  • context 此计算的上下文。
    base 要迭代其键的映射。此解析器仅处理属于 Map 类型的 base。
    return 包含 0 个或多个(可能无限多个)FeatureDescriptor 对象的 Iterator,每个对象代表此映射中的一个键;如果 base 对象不是映射,则返回 null
    英文文档:

    getFeatureDescriptors

    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
                                                             Object base)
    If the base object is a map, returns an Iterator containing the set of keys available in the Map. Otherwise, returns null.

    The Iterator returned must contain zero or more instances of FeatureDescriptor. Each info object contains information about a key in the Map, and is initialized as follows:

  • displayName - The return value of calling the toString method on this key, or "null" if the key is null.
  • name - Same as displayName property.
  • shortDescription - Empty string
  • expert - false
  • hidden - false
  • preferred - true
  • In addition, the following named attributes must be set in the returned FeatureDescriptors:
  • ELResolver.TYPE - The return value of calling the getClass() method on this key, or null if the key is null.
  • ELResolver.RESOLVABLE_AT_DESIGN_TIME - true
  • Specified by:
    getFeatureDescriptors in class ELResolver
    Parameters:
    context - The context of this evaluation.
    base - The map whose keys are to be iterated over. Only bases of type Map are handled by this resolver.
    Returns:
    An Iterator containing zero or more (possibly infinitely more) FeatureDescriptor objects, each representing a key in this map, or null if the base object is not a map.
    See Also:
    FeatureDescriptor

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

    假定 base 为 Map,则此方法将总是返回 Object.class。这是因为 Map 接受任何对象作为键。

    context 此计算的上下文。
    base 要分析的映射。此解析器仅处理属于 Map 类型的 base。
    return 如果 base 不为 Map,则返回 null;否则返回 Object.class
    英文文档:

    getCommonPropertyType

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

    Assuming the base is a Map, this method will always return Object.class. This is because Maps accept any object as a key.

    Specified by:
    getCommonPropertyType in class ELResolver
    Parameters:
    context - The context of this evaluation.
    base - The map to analyze. Only bases of type Map are handled by this resolver.
    Returns:
    null if base is not a Map; otherwise Object.class.


    Submit a bug or feature

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

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

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