EnumConverter (Java EE 5)

Java EE API


javax.faces.convert Class EnumConverter

java.lang.Object
  extended by javax.faces.convert.EnumConverter
All Implemented Interfaces:
StateHolder, Converter

public class EnumConverter
extends Object
implements Converter, StateHolder

Implements: Converter, StateHolder

java.lang.Enum(以及 enum 基本类型)值的 Converter 实现。

英文文档:

Converter implementation for java.lang.Enum (and enum primitive) values.

Since:
1.2

Field Summary
static String
static String
static String
 
Constructor Summary
 
Method Summary
 Object
 String
 boolean
 void
 Object
 void
 
Methods inherited from class java.lang.Object
 

Field Detail

英文文档:

CONVERTER_ID

public static final String CONVERTER_ID

The standard converter id for this converter.

See Also:
Constant Field Values


英文文档:

ENUM_ID

public static final String ENUM_ID

The message identifier of the FacesMessage to be created if the conversion to Enum fails. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the unconverted value.
  • {1} replaced by one of the enum constants or the empty string if none can be found.
  • {2} replaced by a String whose value is the label of the input component that produced this message.

See Also:
Constant Field Values


英文文档:

ENUM_NO_CLASS_ID

public static final String ENUM_NO_CLASS_ID

The message identifier of the FacesMessage to be created if the conversion to Enum fails and no target class has been provided. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the unconverted value.
  • {1} replaced by a String whose value is the label of the input component that produced this message.

See Also:
Constant Field Values

Constructor Detail

public EnumConverter()
英文文档:

EnumConverter

public EnumConverter()

public EnumConverter(Class<T> targetClass)
英文文档:

EnumConverter

public EnumConverter(Class targetClass)
Method Detail

public Object getAsObject(FacesContext context, UIComponent component, String value)

value 参数转换为在构造方法中提供的类枚举常量之一。如果没有向此实例的构造方法提供任何目标类参数,则抛出包含 #ENUM_NO_CLASS_ID 消息和相应参数的 ConverterException。如果 value 参数为 null 或它的长度为 0,则返回 null。否则,使用目标类和 value 执行等效的 Enum.valueOf 并返回 Object。如果转换失败,则抛出一个 ConverterException,它包含 #ENUM_ID 消息和相应的参数。

context 此请求的 FacesContext
component 将应用此值的 UIComponent
value 要转换为 Object 的 String value
ThrowsConverterException: NullPointerException 如果无法成功执行转换
ThrowsNullPointerException: NullPointerException 如果 contextcomponentnull
英文文档:

getAsObject

public Object getAsObject(FacesContext context,
                          UIComponent component,
                          String value)

Convert the value argument to one of the enum constants of the class provided in our constructor. If no target class argument has been provided to the constructor of this instance, throw a ConverterException containing the ENUM_NO_CLASS_ID message with proper parameters. If the value argument is null or it has a length of zero, return null. Otherwise, perform the equivalent of Enum.valueOf using target class and value and return the Object. If the conversion fails, throw a ConverterException containing the ENUM_ID message with proper parameters.

Specified by:
getAsObject in interface Converter
Parameters:
context - the FacesContext for this request.
component - the UIComponent to which this value will be applied.
value - the String value to be converted to Object.
Returns:
null if the value to convert is null, otherwise the result of the conversion
Throws:
ConverterException - if conversion cannot be successfully performed
NullPointerException - if context or component is null

public String getAsString(FacesContext context, UIComponent component, Object value)

将由 value 参数给定的枚举常量转换为 String。如果没有向此实例的构造方法提供任何目标类参数,则抛出包含 #ENUM_NO_CLASS_ID 消息和相应参数的 ConverterException。如果 value 参数为 null,则返回 null。如果该值为提供的目标类的实例,则返回它的字符串值 (value.toString())。否则,抛出一个 ConverterException,它包含 #ENUM_ID 消息和相应的参数。

ThrowsConverterException: NullPointerException 如果无法成功执行转换
ThrowsNullPointerException: NullPointerException 如果 contextcomponentnull
英文文档:

getAsString

public String getAsString(FacesContext context,
                          UIComponent component,
                          Object value)

Convert the enum constant given by the value argument into a String. If no target class argument has been provided to the constructor of this instance, throw a ConverterException containing the ENUM_NO_CLASS_ID message with proper parameters. If the value,/code> argument is null, return null. If the value is an instance of the provided target class, return its string value (value.toString()). Otherwise, throw a ConverterException containing the ENUM_ID message with proper parameters.

Specified by:
getAsString in interface Converter
Parameters:
context - FacesContext for the request being processed
component - UIComponent with which this model object value is associated
value - Model object value to be converted (may be null)
Returns:
a zero-length String if value is null, otherwise the result of the conversion
Throws:
ConverterException - if conversion cannot be successfully performed
NullPointerException - if context or component is null

public void restoreState(FacesContext facesContext, Object object)
英文文档:

restoreState

public void restoreState(FacesContext facesContext,
                         Object object)
Description copied from interface: StateHolder

Perform any processing required to restore the state from the entries in the state Object.

If the class that implements this interface has references to instances that also implement StateHolder (such as a UIComponent with event handlers, validators, etc.) this method must call the StateHolder.restoreState(javax.faces.context.FacesContext, java.lang.Object) method on all those instances as well.

Specified by:
restoreState in interface StateHolder

public Object saveState(FacesContext facesContext)
英文文档:

saveState

public Object saveState(FacesContext facesContext)
Description copied from interface: StateHolder

Gets the state of the instance as a Serializable Object.

If the class that implements this interface has references to instances that implement StateHolder (such as a UIComponent with event handlers, validators, etc.) this method must call the StateHolder.saveState(javax.faces.context.FacesContext) method on all those instances as well. This method must not save the state of children and facets. That is done via the StateManager

This method must not alter the state of the implementing object. In other words, after executing this code:

 Object state = component.saveState(facesContext);
 

component should be the same as before executing it.

The return from this method must be Serializable

Specified by:
saveState in interface StateHolder

public void setTransient(boolean b)
英文文档:

setTransient

public void setTransient(boolean b)
Description copied from interface: StateHolder

Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.

Specified by:
setTransient in interface StateHolder
Parameters:
b - boolean pass true if this Object will participate in state saving or restoring, otherwise pass false.

public boolean isTransient()
英文文档:

isTransient

public boolean isTransient()
Description copied from interface: StateHolder

If true, the Object implementing this interface must not participate in state saving or restoring.

Specified by:
isTransient in interface StateHolder


Submit a bug or feature

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

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

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