|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
javax.faces.convert Class NumberConverter
java.lang.Object javax.faces.convert.NumberConverter
- All Implemented Interfaces:
- StateHolder, Converter
public class NumberConverter
- extends Object
- implements Converter, StateHolder
java.lang.Number
值的 Converter
实现。
getAsObject()
方法根据以下算法将 String 解析为 java.lang.Double
或 java.lang.Long
:
- 如果指定的 String 为 null,则返回
null
。否则,在继续执行前去除前导空格和尾部的空格。 - 如果指定的 String(去除空格后)长度为 0,则返回
null
。 - 如果
locale
属性不为 null,则使用该Locale
管理解析。否则,使用UIViewRoot
的Locale
。 - 如果指定了
pattern
,则其语法必须遵守java.text.DecimalFormat
指定的规则。此类模式将用于解析,并且将忽略type
属性。 - 如果尚未指定
pattern
,则将根据type
属性进行解析,它应该为货币、数字或百分比。通过调用java.text.NumberFormat
类的getCurrencyInstance()
、getNumberInstance()
或getPercentInstance()
方法,并传入选择的Locale
,可以分别确定货币、数字和百分比的解析模式。 - 如果
integerOnly
属性设置为 true,则将仅解析 String 的整数部分。有关详细信息,请参见java.text.NumberFormat
类的setParseIntegerOnly()
方法的 JavaDoc。
getAsString()
方法应该获取类型为 java.lang.Number
(或子类)的值,并根据以下算法创建格式化 String:
- 如果指定的值为 null,则返回长度为 0 的 String。
- 如果指定的值为 String,则不加修改地返回该值。
- 如果
locale
属性不为 null,则使用该Locale
管理格式化。否则,使用FacesContext
中的Locale
。 - 如果指定了
pattern
,则其语法必须遵守java.text.DecimalFormat
指定的规则。此类模式将用于格式化,并且将忽略type
属性(以及在下一段中描述的有关格式化的选项)。 - 如果尚未指定
pattern
,则将根据type
属性进行格式化,它将该值格式化为货币、数字或百分比。通过调用java.text.NumberFormat
类的getCurrencyInstance()
、getNumberInstance()
或getPercentInstance()
方法,并传入选择的Locale
,可以分别确定货币、数字和百分比的格式模式。此外,将对该格式模式应用以下属性(如果指定):- 如果
groupingUsed
属性为true
,则对相应的NumberFormat
实例调用setGroupingUsed(true)
方法。 - 将根据为
maxFractionDigits
、maxIntegerDigits
、minFractionDigits
和minIntegerDigits
属性设置的任何值来配置结果整数部分和小数部分的最小位数和最大位数。 - 如果类型设置为
currency
,也可以使用currencyCode
或currencySymbol
属性配置要使用的货币符号。如果设置了两者,则currencyCode
的值优先于 JDK 1.4(或更高版本)JVM;否则currencySymbol
的值优先。
- 如果
Converter
implementation for java.lang.Number
values.
The getAsObject()
method parses a String into an
java.lang.Double
or java.lang.Long
, according
to the following algorithm:
- If the specified String is null, return
a
null
. Otherwise, trim leading and trailing whitespace before proceeding. - If the specified String - after trimming - has a zero length,
return
null
. - If the
locale
property is not null, use thatLocale
for managing parsing. Otherwise, use theLocale
from theUIViewRoot
. - If a
pattern
has been specified, its syntax must conform the rules specified byjava.text.DecimalFormat
. Such a pattern will be used to parse, and thetype
property will be ignored. - If a
pattern
has not been specified, parsing will be based on thetype
property, which expects a currency, a number, or a percent. The parse pattern for currencies, numbers, and percentages is determined by calling thegetCurrencyInstance()
,getNumberInstance()
, orgetPercentInstance()
method of thejava.text.NumberFormat
class, passing in the selectedLocale
. - If the
integerOnly
property has been set to true, only the integer portion of the String will be parsed. See the JavaDocs for thesetParseIntegerOnly()
method of thejava.text.NumberFormat
class for more information.
The getAsString()
method expects a value of type
java.lang.Number
(or a subclass), and creates a formatted
String according to the following algorithm:
- If the specified value is null, return a zero-length String.
- If the specified value is a String, return it unmodified.
- If the
locale
property is not null, use thatLocale
for managing formatting. Otherwise, use theLocale
from theFacesContext
. - If a
pattern
has been specified, its syntax must conform the rules specified byjava.text.DecimalFormat
. Such a pattern will be used to format, and thetype
property (along with related formatting options described in the next paragraph) will be ignored. - If a
pattern
has not been specified, formatting will be based on thetype
property, which formats the value as a currency, a number, or a percent. The format pattern for currencies, numbers, and percentages is determined by calling the percentages is determined by calling thegetCurrencyInstance()
,getNumberInstance()
, orgetPercentInstance()
method of thejava.text.NumberFormat
class, passing in the selectedLocale
. In addition, the following properties will be applied to the format pattern, if specified:- If the
groupingUsed
property istrue
, thesetGroupingUsed(true)
method on the correspondingNumberFormat
instance will be called. - The minimum and maximum number of digits in the integer and
fractional portions of the result will be configured based on
any values set for the
maxFractionDigits
,maxIntegerDigits
,minFractionDigits
, andminIntegerDigits
properties. - If the type is set to
currency
, it is also possible to configure the currency symbol to be used, using either thecurrencyCode
orcurrencySymbol
properties. If both are set, the value forcurrencyCode
takes precedence on a JDK 1.4 (or later) JVM; otherwise, the value forcurrencySymbol
takes precedence.
- If the
Field Summary | |
---|---|
static String |
CONVERTER_ID
The standard converter id for this converter. |
static String |
CURRENCY_ID
The message identifier of the FacesMessage to be created if
the conversion to Number fails. |
static String |
NUMBER_ID
The message identifier of the FacesMessage to be created if
the conversion to Number fails. |
static String |
PATTERN_ID
The message identifier of the FacesMessage to be created if
the conversion to Number fails. |
static String |
PERCENT_ID
The message identifier of the FacesMessage to be created if
the conversion to Number fails. |
static String |
STRING_ID
The message identifier of the FacesMessage to be created if
the conversion of the Number value to
String fails. |
Constructor Summary | |
---|---|
NumberConverter()
|
Method Summary | |
---|---|
Object |
getAsObject(FacesContext context,
UIComponent component,
String value)
Convert the specified string value, which is associated with the specified UIComponent , into a model data object that
is appropriate for being stored during the Apply Request
Values phase of the request processing lifecycle. |
String |
getAsString(FacesContext context,
UIComponent component,
Object value)
Convert the specified model object value, which is associated with the specified UIComponent , into a String that is suitable
for being included in the response generated during the
Render Response phase of the request processing
lifeycle. |
String |
getCurrencyCode()
Return the ISO 4217 currency code used by getAsString()
with a type of currency . |
String |
getCurrencySymbol()
Return the currency symbol used by getAsString()
with a type of currency . |
Locale |
getLocale()
Return the Locale to be used when parsing numbers. |
int |
getMaxFractionDigits()
Return the maximum number of digits getAsString() should
render in the fraction portion of the result. |
int |
getMaxIntegerDigits()
Return the maximum number of digits getAsString() should
render in the integer portion of the result. |
int |
getMinFractionDigits()
Return the minimum number of digits getAsString() should
render in the fraction portion of the result. |
int |
getMinIntegerDigits()
Return the minimum number of digits getAsString() should
render in the integer portion of the result. |
String |
getPattern()
Return the format pattern to be used when formatting and parsing numbers. |
String |
getType()
Return the number type to be used when formatting and parsing numbers. |
boolean |
isGroupingUsed()
Return true if getAsString should include
grouping separators if necessary. |
boolean |
isIntegerOnly()
Return true if only the integer portion of the given
value should be returned from getAsObject() . |
boolean |
isTransient()
If true, the Object implementing this interface must not participate in state saving or restoring. |
void |
restoreState(FacesContext context,
Object state)
Perform any processing required to restore the state from the entries in the state Object. |
Object |
saveState(FacesContext context)
Gets the state of the instance as a Serializable Object. |
void |
setCurrencyCode(String currencyCode)
Set the ISO 4217 currency code used by getAsString()
with a type of currency . |
void |
setCurrencySymbol(String currencySymbol)
Set the currency symbol used by getAsString()
with a type of currency . |
void |
setGroupingUsed(boolean groupingUsed)
Set the flag indicating whether getAsString() should
include grouping separators if necessary. |
void |
setIntegerOnly(boolean integerOnly)
Set to true if only the integer portion of the given
value should be returned from getAsObject() . |
void |
setLocale(Locale locale)
Set the Locale to be used when parsing numbers. |
void |
setMaxFractionDigits(int maxFractionDigits)
Set the maximum number of digits getAsString() should
render in the fraction portion of the result. |
void |
setMaxIntegerDigits(int maxIntegerDigits)
Set the maximum number of digits getAsString() should
render in the integer portion of the result. |
void |
setMinFractionDigits(int minFractionDigits)
Set the minimum number of digits getAsString() should
render in the fraction portion of the result. |
void |
setMinIntegerDigits(int minIntegerDigits)
Set the minimum number of digits getAsString() should
render in the integer portion of the result. |
void |
setPattern(String pattern)
Set the format pattern to be used when formatting and parsing numbers. |
void |
setTransient(boolean transientFlag)
Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring. |
void |
setType(String type)
Set the number type to be used when formatting and parsing numbers. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
英文文档:
CONVERTER_ID
public static final String CONVERTER_ID
The standard converter id for this converter.
- See Also:
- Constant Field Values
英文文档:
CURRENCY_ID
public static final String CURRENCY_ID
The message identifier of the
FacesMessage
to be created if the conversion toNumber
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
英文文档:
NUMBER_ID
public static final String NUMBER_ID
The message identifier of the
FacesMessage
to be created if the conversion toNumber
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
英文文档:
PATTERN_ID
public static final String PATTERN_ID
The message identifier of the
FacesMessage
to be created if the conversion toNumber
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
英文文档:
PERCENT_ID
public static final String PERCENT_ID
The message identifier of the
FacesMessage
to be created if the conversion toNumber
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
英文文档:
STRING_ID
public static final String STRING_ID
The message identifier of the
FacesMessage
to be created if the conversion of theNumber
value toString
fails. The message format string for this message may optionally include the following placeholders:{0}
relaced by the unconverted value.{1}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
Constructor Detail |
---|
public
NumberConverter()
英文文档:
NumberConverter
public NumberConverter()
Method Detail |
---|
public String
getCurrencyCode()
返回 type
为 currency
的 getAsString()
所使用的 ISO 4217 货币代码。如果未设置,则使用的值将根据格式 Locale
确定。
getCurrencyCode
public String getCurrencyCode()
Return the ISO 4217 currency code used by
getAsString()
with atype
ofcurrency
. If not set, the value used will be based on the formattingLocale
.
public void
setCurrencyCode(String currencyCode)
设置 type
为 currency
的 getAsString()
所使用的 ISO 4217 货币代码。
currencyCode | 新货币代码 |
setCurrencyCode
public void setCurrencyCode(String currencyCode)
Set the ISO 4217 currency code used by
getAsString()
with atype
ofcurrency
.- Parameters:
currencyCode
- The new currency code
public String
getCurrencySymbol()
返回 type
为 currency
的 getAsString()
所使用的货币符号。如果未设置,则使用的值将根据格式 Locale
确定。
getCurrencySymbol
public String getCurrencySymbol()
Return the currency symbol used by
getAsString()
with atype
ofcurrency
. If not set, the value used will be based on the formattingLocale
.
public void
setCurrencySymbol(String currencySymbol)
设置 type
为 currency
的 getAsString()
所使用的货币符号。
currencySymbol | 新货币符号 |
setCurrencySymbol
public void setCurrencySymbol(String currencySymbol)
Set the currency symbol used by
getAsString()
with atype
ofcurrency
.- Parameters:
currencySymbol
- The new currency symbol
public boolean
isGroupingUsed()
如果 getAsString
应该包含分组分隔符(如有必要),则返回 true
。如果未修改,则默认值为 true
。
isGroupingUsed
public boolean isGroupingUsed()
Return
true
ifgetAsString
should include grouping separators if necessary. If not modified, the default value istrue
.
public void
setGroupingUsed(boolean groupingUsed)
设置指示 getAsString()
是否应该包含分组分隔符(如有必要)的标志。
groupingUsed | 新的分组使用的标志 |
setGroupingUsed
public void setGroupingUsed(boolean groupingUsed)
Set the flag indicating whether
getAsString()
should include grouping separators if necessary.- Parameters:
groupingUsed
- The new grouping used flag
public boolean
isIntegerOnly()
仅当给定值的整数部分应该从 getAsObject()
返回时,返回 true
。如果未修改,则默认值为 false
。
isIntegerOnly
public boolean isIntegerOnly()
Return
true
if only the integer portion of the given value should be returned fromgetAsObject()
. If not modified, the default value isfalse
.
public void
setIntegerOnly(boolean integerOnly)
仅当给定值的整数部分应该从 getAsObject()
返回时,设置为 true
。
integerOnly | 新的 integer-only 标志 |
setIntegerOnly
public void setIntegerOnly(boolean integerOnly)
Set to
true
if only the integer portion of the given value should be returned fromgetAsObject()
.- Parameters:
integerOnly
- The new integer-only flag
public int
getMaxFractionDigits()
返回 getAsString()
应该在结果中呈现的小数部分的最大位数。
getMaxFractionDigits
public int getMaxFractionDigits()
Return the maximum number of digits
getAsString()
should render in the fraction portion of the result.
public void
setMaxFractionDigits(int maxFractionDigits)
设置 getAsString()
应该在结果中呈现的小数部分的最大位数。如果未设置,则数值将取决于要转换的值。
maxFractionDigits | 新的限制 |
setMaxFractionDigits
public void setMaxFractionDigits(int maxFractionDigits)
Set the maximum number of digits
getAsString()
should render in the fraction portion of the result. If not set, the number of digits depends on the value being converted.- Parameters:
maxFractionDigits
- The new limit
public int
getMaxIntegerDigits()
返回 getAsString()
应该在结果中呈现的整数部分的最大位数。
getMaxIntegerDigits
public int getMaxIntegerDigits()
Return the maximum number of digits
getAsString()
should render in the integer portion of the result.
public void
setMaxIntegerDigits(int maxIntegerDigits)
设置 getAsString()
应该在结果中呈现的整数部分的最大位数。如果未设置,则数值将取决于要转换的值。
maxIntegerDigits | 新的限制 |
setMaxIntegerDigits
public void setMaxIntegerDigits(int maxIntegerDigits)
Set the maximum number of digits
getAsString()
should render in the integer portion of the result. If not set, the number of digits depends on the value being converted.- Parameters:
maxIntegerDigits
- The new limit
public int
getMinFractionDigits()
返回 getAsString()
应该在结果中呈现的小数部分的最小位数。
getMinFractionDigits
public int getMinFractionDigits()
Return the minimum number of digits
getAsString()
should render in the fraction portion of the result.
public void
setMinFractionDigits(int minFractionDigits)
设置 getAsString()
应该在结果中呈现的小数部分的最小位数。如果未设置,则数值将取决于要转换的值。
minFractionDigits | 新的限制 |
setMinFractionDigits
public void setMinFractionDigits(int minFractionDigits)
Set the minimum number of digits
getAsString()
should render in the fraction portion of the result. If not set, the number of digits depends on the value being converted.- Parameters:
minFractionDigits
- The new limit
public int
getMinIntegerDigits()
返回 getAsString()
应该在结果中呈现的整数部分的最小位数。
getMinIntegerDigits
public int getMinIntegerDigits()
Return the minimum number of digits
getAsString()
should render in the integer portion of the result.
public void
setMinIntegerDigits(int minIntegerDigits)
设置 getAsString()
应该在结果中呈现的整数部分的最小位数。如果未设置,则数值将取决于要转换的值。
minIntegerDigits | 新的限制 |
setMinIntegerDigits
public void setMinIntegerDigits(int minIntegerDigits)
Set the minimum number of digits
getAsString()
should render in the integer portion of the result. If not set, the number of digits depends on the value being converted.- Parameters:
minIntegerDigits
- The new limit
public java.util.Locale
getLocale()
返回解析数字时使用的 Locale
。如果此值为 null
,则将使用存储在当前请求的 javax.faces.component.UIViewRoot
中的 Locale
。
getLocale
public Locale getLocale()
Return the
Locale
to be used when parsing numbers. If this value isnull
, theLocale
stored in theUIViewRoot
for the current request will be utilized.
public void
setLocale(java.util.Locale locale)
设置解析数字时使用的 Locale
。如果设置为 null
,则将使用存储在当前请求的 javax.faces.component.UIViewRoot
中的 Locale
。
locale |
新的 Locale (或者 null )。 |
setLocale
public void setLocale(Locale locale)
Set the
Locale
to be used when parsing numbers. If set tonull
, theLocale
stored in theUIViewRoot
for the current request will be utilized.- Parameters:
locale
- The newLocale
(ornull
)
public String
getPattern()
返回将在格式化和解析数字时使用的格式模式。
getPattern
public String getPattern()
Return the format pattern to be used when formatting and parsing numbers.
public void
setPattern(String pattern)
设置将在格式化和解析数字时使用的格式模式。有效值是 java.text.DecimalFormat
支持的那些值。当调用 getAsObject()
或 getAsString()
时,无效值将导致 ConverterException
。
pattern | 新的格式模式 |
setPattern
public void setPattern(String pattern)
Set the format pattern to be used when formatting and parsing numbers. Valid values are those supported by
java.text.DecimalFormat
. An invalid value will cause aConverterException
whengetAsObject()
orgetAsString()
is called.- Parameters:
pattern
- The new format pattern
public String
getType()
返回将在格式化和解析数字时使用的数字类型。如果未修改,则默认类型为 number
。
getType
public String getType()
Return the number type to be used when formatting and parsing numbers. If not modified, the default type is
number
.
public void
setType(String type)
设置将在格式化和解析数字时使用的数字类型。有效值有 currency
、number
或 percent
。当调用 getAsObject()
或 getAsString()
时,无效值将导致 ConverterException
。
type | 新的数字样式 |
setType
public void setType(String type)
Set the number type to be used when formatting and parsing numbers. Valid values are
currency
,number
, orpercent
. An invalid value will cause aConverterException
whengetAsObject()
orgetAsString()
is called.- Parameters:
type
- The new number style
public Object
getAsObject(FacesContext context, UIComponent component, String value)
Throws | ConverterException: NullPointerException 如果无法成功执行转换 |
Throws | NullPointerException:
NullPointerException
如果 context 或 component 为 null |
getAsObject
public Object getAsObject(FacesContext context, UIComponent component, String value)
- Description copied from interface:
Converter
Convert the specified string value, which is associated with the specified
UIComponent
, into a model data object that is appropriate for being stored during the Apply Request Values phase of the request processing lifecycle.- Specified by:
getAsObject
in interfaceConverter
- Parameters:
context
-FacesContext
for the request being processedcomponent
-UIComponent
with which this model object value is associatedvalue
- String value to be converted (may benull
)- Returns:
null
if the value to convert isnull
, otherwise the result of the conversion- Throws:
ConverterException
- if conversion cannot be successfully performedNullPointerException
- ifcontext
orcomponent
isnull
public String
getAsString(FacesContext context, UIComponent component, Object value)
Throws | ConverterException: NullPointerException 如果无法成功执行转换 |
Throws | NullPointerException:
NullPointerException
如果 context 或 component 为 null |
getAsString
public String getAsString(FacesContext context, UIComponent component, Object value)
- Description copied from interface:
Converter
Convert the specified model object value, which is associated with the specified
UIComponent
, into a String that is suitable for being included in the response generated during the Render Response phase of the request processing lifeycle.- Specified by:
getAsString
in interfaceConverter
- Parameters:
context
-FacesContext
for the request being processedcomponent
-UIComponent
with which this model object value is associatedvalue
- Model object value to be converted (may benull
)- Returns:
- a zero-length String if value is
null
, otherwise the result of the conversion - Throws:
ConverterException
- if conversion cannot be successfully performedNullPointerException
- ifcontext
orcomponent
isnull
public Object
saveState(FacesContext context)
英文文档:
saveState
public Object saveState(FacesContext context)
- 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 theStateHolder.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 theStateManager
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 interfaceStateHolder
public void
restoreState(FacesContext context, Object state)
英文文档:
restoreState
public void restoreState(FacesContext context, Object state)
- 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 theStateHolder.restoreState(javax.faces.context.FacesContext, java.lang.Object)
method on all those instances as well.- Specified by:
restoreState
in interfaceStateHolder
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 interfaceStateHolder
public void
setTransient(boolean transientFlag)
英文文档:
setTransient
public void setTransient(boolean transientFlag)
- 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 interfaceStateHolder
- Parameters:
transientFlag
- boolean passtrue
if this Object will participate in state saving or restoring, otherwise passfalse
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Submit a bug or feature
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!