LengthValidator (Java EE 5)

Java EE API


javax.faces.validator Class LengthValidator

java.lang.Object
  extended by javax.faces.validator.LengthValidator
All Implemented Interfaces:
EventListener, StateHolder, Validator

public class LengthValidator
extends Object
implements Validator, StateHolder

Implements: Validator, StateHolder

LengthValidator 是一个 Validator,它检查关联组件值的 String 表示形式中的字符数。实现以下算法:

  • 如有必要,可调用其 toString 方法将传递的值转换为 String。
  • 如果对此 Validator 配置了 maximum 属性,则根据此限制检查转换的 String。如果 String 长度大于指定的最大值,则抛出包含 MAXIMUM_MESSAGE_ID 消息的 ValidatorException
  • 如果对此 Validator 配置了 minimum 属性,则根据此限制检查转换的 String。如果 String 长度小于指定的最小值,则抛出包含 MINIMUM_MESSAGE_ID 消息的 ValidatorException

对于所有导致抛出 ValidatorException 的上述原因,如果有些消息的参数匹配验证器参数,则这些参数的值必须使用转换器 ID javax.faces.Number 在应用程序中注册的 Converter 转换。这使值可以根据当前的 Locale 进行本地化。

英文文档:

LengthValidator is a Validator that checks the number of characters in the String representation of the value of the associated component. The following algorithm is implemented:

  • Convert the passed value to a String, if necessary, by calling its toString() method.
  • If a maximum property has been configured on this Validator, check the length of the converted String against this limit. If the String length is larger than the specified maximum, throw a ValidatorException containing a a MAXIMUM_MESSAGE_ID message.
  • If a minimum property has been configured on this Validator, check the length of the converted String against this limit. If the String length is less than the specified minimum, throw a ValidatorException containing a a MINIMUM_MESSAGE_ID message.

For all of the above cases that cause a ValidatorException to be thrown, if there are parameters to the message that match up with validator parameters, the values of these parameters must be converted using the Converter registered in the application under the converter id javax.faces.Number. This allows the values to be localized according to the current Locale.


Field Summary
static String
static String
static String
 
Fields inherited from interface javax.faces.validator.Validator
 
Constructor Summary
 
Method Summary
 boolean
 int
 int
 int
 boolean
 void
 Object
 void
 void
 void
 void
 
Methods inherited from class java.lang.Object
 

Field Detail

英文文档:

VALIDATOR_ID

public static final String VALIDATOR_ID

The standard validator id for this validator.

See Also:
Constant Field Values


英文文档:

MAXIMUM_MESSAGE_ID

public static final String MAXIMUM_MESSAGE_ID

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

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

See Also:
Constant Field Values


英文文档:

MINIMUM_MESSAGE_ID

public static final String MINIMUM_MESSAGE_ID

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

  • {0} replaced by the configured minimum length.
  • {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 LengthValidator()

构造没有预配置限制的 Validator

英文文档:

LengthValidator

public LengthValidator()

Construct a Validator with no preconfigured limits.


public LengthValidator(int maximum)

构造具有指定预配置限制的 Validator

maximum 允许的最大值
英文文档:

LengthValidator

public LengthValidator(int maximum)

Construct a Validator with the specified preconfigured limit.

Parameters:
maximum - Maximum value to allow


public LengthValidator(int maximum, int minimum)

构造具有指定预配置限制的 Validator

maximum 允许的最大值
minimum 允许的最小值
英文文档:

LengthValidator

public LengthValidator(int maximum,
                       int minimum)

Construct a Validator with the specified preconfigured limits.

Parameters:
maximum - Maximum value to allow
minimum - Minimum value to allow

Method Detail

public int getMaximum()

返回此 Validator 强制执行的最大长度;如果没有设置最大值,则返回 0

英文文档:

getMaximum

public int getMaximum()

Return the maximum length to be enforced by this Validator, or 0 if the maximum has not been set.


public void setMaximum(int maximum)

设置此 Validator 强制执行的最大长度。

maximum 新的最大值
英文文档:

setMaximum

public void setMaximum(int maximum)

Set the maximum length to be enforced by this Validator.

Parameters:
maximum - The new maximum value

public int getMinimum()

返回此 Validator 强制执行的最小长度;如果没有设置最小值,则返回 0

英文文档:

getMinimum

public int getMinimum()

Return the minimum length to be enforced by this Validator, or 0 if the minimum has not been set.


public void setMinimum(int minimum)

设置此 Validator 强制执行的最小长度。

minimum 新的最小值
英文文档:

setMinimum

public void setMinimum(int minimum)

Set the minimum length to be enforced by this Validator.

Parameters:
minimum - The new minimum value

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
ThrowsNullPointerException: NullPointerException 如果 contextcomponentnull
ThrowsValidatorException: NullPointerException 如果验证失败
英文文档:

validate

public void validate(FacesContext context,
                     UIComponent component,
                     Object value)
              throws ValidatorException
Description copied from interface: Validator

Perform the correctness checks implemented by this Validator against the specified UIComponent. If any violations are found, a ValidatorException will be thrown containing the FacesMessage describing the failure.

Specified by:
validate in interface Validator
Parameters:
context - FacesContext for the request we are processing
component - UIComponent we are checking for correctness
value - the value to validate
Throws:
NullPointerException - if context or component is null
ValidatorException - if validation fails

public boolean equals(Object otherObj)
英文文档:

equals

public boolean equals(Object otherObj)
Overrides:
equals in class Object

public int hashCode()
英文文档:

hashCode

public int hashCode()
Overrides:
hashCode in class Object

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 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 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 the StateHolder.restoreState(javax.faces.context.FacesContext, java.lang.Object) method on all those instances as well.

Specified by:
restoreState in interface StateHolder

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

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

setTransient

public void setTransient(boolean transientValue)
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:
transientValue - boolean pass true if this Object will participate in state saving or restoring, otherwise pass false.


Submit a bug or feature

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

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

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