Class BindException
- java.lang.Object
- java.lang.Throwable
- java.lang.Exception
- org.springframework.validation.BindException
- All Implemented Interfaces:
Serializable,BindingResult,Errors
public class BindException extends Exception implements BindingResult
Thrown when binding errors are considered fatal. Implements theBindingResultinterface (and its super-interfaceErrors) to allow for the direct analysis of binding errors.As of Spring 2.0, this is a special-purpose class. Normally, application code will work with the
BindingResultinterface, or with aDataBinderthat in turn exposes a BindingResult viaDataBinder.getBindingResult().- Author:
- Rod Johnson, Juergen Hoeller, Rob Harrop
- See Also:
BindingResult,DataBinder.getBindingResult(),DataBinder.close(), Serialized Form
Field Summary
Fields inherited from interface org.springframework.validation.BindingResult
MODEL_KEY_PREFIX
Fields inherited from interface org.springframework.validation.Errors
NESTED_PATH_SEPARATOR
Constructor Summary
Constructors Constructor Description BindException(Object target, String objectName)Create a new BindException instance for a target bean.BindException(BindingResult bindingResult)Create a new BindException instance for a BindingResult.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAllErrors(Errors errors)Add all errors from the givenErrorsinstance to thisErrorsinstance.voidaddError(ObjectError error)Add a customObjectErrororFieldErrorto the errors list.booleanequals(Object other)PropertyEditorfindEditor(String field, Class valueType)Find a custom property editor for the given type and property.List<ObjectError>getAllErrors()Get all errors, both global and field ones.BindingResultgetBindingResult()Return the BindingResult that this BindException wraps.intgetErrorCount()Return the total number of errors.FieldErrorgetFieldError()Get the first error associated with a field, if any.FieldErrorgetFieldError(String field)Get the first error associated with the given field, if any.intgetFieldErrorCount()Return the number of errors associated with a field.intgetFieldErrorCount(String field)Return the number of errors associated with the given field.List<FieldError>getFieldErrors()Get all errors associated with a field.List<FieldError>getFieldErrors(String field)Get all errors associated with the given field.Class<?>getFieldType(String field)Return the type of a given field.ObjectgetFieldValue(String field)Return the current value of the given field, either the current bean property value or a rejected update from the last binding.ObjectErrorgetGlobalError()Get the first global error, if any.intgetGlobalErrorCount()Return the number of global errors.List<ObjectError>getGlobalErrors()Get all global errors.StringgetMessage()Returns diagnostic information about the errors held in this object.Map<String,Object>getModel()Return a model Map for the obtained state, exposing a BindingResult instance as 'MODEL_KEY_PREFIX+ objectName' and the object itself as 'objectName'.StringgetNestedPath()Return the current nested path of thisErrorsobject.StringgetObjectName()Return the name of the bound root object.PropertyEditorRegistrygetPropertyEditorRegistry()Return the underlying PropertyEditorRegistry.ObjectgetRawFieldValue(String field)Extract the raw field value for the given field.String[]getSuppressedFields()Return the list of fields that were suppressed during the bind process.ObjectgetTarget()Return the wrapped target object, which may be a bean, an object with public fields, a Map - depending on the concrete binding strategy.booleanhasErrors()Return if there were any errors.booleanhasFieldErrors()Are there any field errors?booleanhasFieldErrors(String field)Are there any errors associated with the given field?booleanhasGlobalErrors()Are there any global errors?inthashCode()voidpopNestedPath()Pop the former nested path from the nested path stack.voidpushNestedPath(String subPath)Push the given sub path onto the nested path stack.voidrecordFieldValue(String field, Class<?> type, Object value)Record the given value for the specified field.voidrecordSuppressedField(String field)Mark the specified disallowed field as suppressed.voidreject(String errorCode)Register a global error for the entire target object, using the given error description.voidreject(String errorCode, Object[] errorArgs, String defaultMessage)Register a global error for the entire target object, using the given error description.voidreject(String errorCode, String defaultMessage)Register a global error for the entire target object, using the given error description.voidrejectValue(String field, String errorCode)Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.voidrejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage)Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.voidrejectValue(String field, String errorCode, String defaultMessage)Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.String[]resolveMessageCodes(String errorCode)Resolve the given error code into message codes.String[]resolveMessageCodes(String errorCode, String field)Resolve the given error code into message codes for the given field.voidsetNestedPath(String nestedPath)Allow context to be changed so that standard validators can validate subtrees.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Constructor Detail
BindException
public BindException(BindingResult bindingResult)
Create a new BindException instance for a BindingResult.- Parameters:
bindingResult- the BindingResult instance to wrap
BindException
public BindException(Object target, String objectName)
Create a new BindException instance for a target bean.- Parameters:
target- the target bean to bind ontoobjectName- the name of the target object- See Also:
BeanPropertyBindingResult
Method Detail
getBindingResult
public final BindingResult getBindingResult()
Return the BindingResult that this BindException wraps.
getObjectName
public String getObjectName()
Description copied from interface:ErrorsReturn the name of the bound root object.- Specified by:
getObjectNamein interfaceErrors
setNestedPath
public void setNestedPath(String nestedPath)
Description copied from interface:ErrorsAllow context to be changed so that standard validators can validate subtrees. Reject calls prepend the given path to the field names.For example, an address validator could validate the subobject "address" of a customer object.
- Specified by:
setNestedPathin interfaceErrors- Parameters:
nestedPath- nested path within this object, e.g. "address" (defaults to "",nullis also acceptable). Can end with a dot: both "address" and "address." are valid.
getNestedPath
public String getNestedPath()
Description copied from interface:ErrorsReturn the current nested path of thisErrorsobject.Returns a nested path with a dot, i.e. "address.", for easy building of concatenated paths. Default is an empty String.
- Specified by:
getNestedPathin interfaceErrors
pushNestedPath
public void pushNestedPath(String subPath)
Description copied from interface:ErrorsPush the given sub path onto the nested path stack.A
Errors.popNestedPath()call will reset the original nested path before the correspondingpushNestedPath(String)call.Using the nested path stack allows to set temporary nested paths for subobjects without having to worry about a temporary path holder.
For example: current path "spouse.", pushNestedPath("child") -> result path "spouse.child."; popNestedPath() -> "spouse." again.
- Specified by:
pushNestedPathin interfaceErrors- Parameters:
subPath- the sub path to push onto the nested path stack- See Also:
Errors.popNestedPath()
popNestedPath
public void popNestedPath() throws IllegalStateException
Description copied from interface:ErrorsPop the former nested path from the nested path stack.- Specified by:
popNestedPathin interfaceErrors- Throws:
IllegalStateException- if there is no former nested path on the stack- See Also:
Errors.pushNestedPath(java.lang.String)
reject
public void reject(String errorCode)
Description copied from interface:ErrorsRegister a global error for the entire target object, using the given error description.
reject
public void reject(String errorCode, String defaultMessage)
Description copied from interface:ErrorsRegister a global error for the entire target object, using the given error description.
reject
public void reject(String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage)
Description copied from interface:ErrorsRegister a global error for the entire target object, using the given error description.
rejectValue
public void rejectValue(@Nullable String field, String errorCode)
Description copied from interface:ErrorsRegister a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.The field name may be
nullor empty String to indicate the current object itself rather than a field of it. This may result in a corresponding field error within the nested object graph or a global error if the current object is the top object.- Specified by:
rejectValuein interfaceErrors- Parameters:
field- the field name (may benullor empty String)errorCode- error code, interpretable as a message key- See Also:
Errors.getNestedPath()
rejectValue
public void rejectValue(@Nullable String field, String errorCode, String defaultMessage)
Description copied from interface:ErrorsRegister a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.The field name may be
nullor empty String to indicate the current object itself rather than a field of it. This may result in a corresponding field error within the nested object graph or a global error if the current object is the top object.- Specified by:
rejectValuein interfaceErrors- Parameters:
field- the field name (may benullor empty String)errorCode- error code, interpretable as a message keydefaultMessage- fallback default message- See Also:
Errors.getNestedPath()
rejectValue
public void rejectValue(@Nullable String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage)
Description copied from interface:ErrorsRegister a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.The field name may be
nullor empty String to indicate the current object itself rather than a field of it. This may result in a corresponding field error within the nested object graph or a global error if the current object is the top object.- Specified by:
rejectValuein interfaceErrors- Parameters:
field- the field name (may benullor empty String)errorCode- error code, interpretable as a message keyerrorArgs- error arguments, for argument binding via MessageFormat (can benull)defaultMessage- fallback default message- See Also:
Errors.getNestedPath()
addAllErrors
public void addAllErrors(Errors errors)
Description copied from interface:ErrorsAdd all errors from the givenErrorsinstance to thisErrorsinstance.This is a convenience method to avoid repeated
reject(..)calls for merging anErrorsinstance into anotherErrorsinstance.Note that the passed-in
Errorsinstance is supposed to refer to the same target object, or at least contain compatible errors that apply to the target object of thisErrorsinstance.- Specified by:
addAllErrorsin interfaceErrors- Parameters:
errors- theErrorsinstance to merge in
hasErrors
public boolean hasErrors()
Description copied from interface:ErrorsReturn if there were any errors.
getErrorCount
public int getErrorCount()
Description copied from interface:ErrorsReturn the total number of errors.- Specified by:
getErrorCountin interfaceErrors
getAllErrors
public List<ObjectError> getAllErrors()
Description copied from interface:ErrorsGet all errors, both global and field ones.- Specified by:
getAllErrorsin interfaceErrors- Returns:
- a list of
ObjectErrorinstances
hasGlobalErrors
public boolean hasGlobalErrors()
Description copied from interface:ErrorsAre there any global errors?- Specified by:
hasGlobalErrorsin interfaceErrors- Returns:
trueif there are any global errors- See Also:
Errors.hasFieldErrors()
getGlobalErrorCount
public int getGlobalErrorCount()
Description copied from interface:ErrorsReturn the number of global errors.- Specified by:
getGlobalErrorCountin interfaceErrors- Returns:
- the number of global errors
- See Also:
Errors.getFieldErrorCount()
getGlobalErrors
public List<ObjectError> getGlobalErrors()
Description copied from interface:ErrorsGet all global errors.- Specified by:
getGlobalErrorsin interfaceErrors- Returns:
- a list of
ObjectErrorinstances
getGlobalError
@Nullable public ObjectError getGlobalError()
Description copied from interface:ErrorsGet the first global error, if any.- Specified by:
getGlobalErrorin interfaceErrors- Returns:
- the global error, or
null
hasFieldErrors
public boolean hasFieldErrors()
Description copied from interface:ErrorsAre there any field errors?- Specified by:
hasFieldErrorsin interfaceErrors- Returns:
trueif there are any errors associated with a field- See Also:
Errors.hasGlobalErrors()
getFieldErrorCount
public int getFieldErrorCount()
Description copied from interface:ErrorsReturn the number of errors associated with a field.- Specified by:
getFieldErrorCountin interfaceErrors- Returns:
- the number of errors associated with a field
- See Also:
Errors.getGlobalErrorCount()
getFieldErrors
public List<FieldError> getFieldErrors()
Description copied from interface:ErrorsGet all errors associated with a field.- Specified by:
getFieldErrorsin interfaceErrors- Returns:
- a List of
FieldErrorinstances
getFieldError
@Nullable public FieldError getFieldError()
Description copied from interface:ErrorsGet the first error associated with a field, if any.- Specified by:
getFieldErrorin interfaceErrors- Returns:
- the field-specific error, or
null
hasFieldErrors
public boolean hasFieldErrors(String field)
Description copied from interface:ErrorsAre there any errors associated with the given field?- Specified by:
hasFieldErrorsin interfaceErrors- Parameters:
field- the field name- Returns:
trueif there were any errors associated with the given field
getFieldErrorCount
public int getFieldErrorCount(String field)
Description copied from interface:ErrorsReturn the number of errors associated with the given field.- Specified by:
getFieldErrorCountin interfaceErrors- Parameters:
field- the field name- Returns:
- the number of errors associated with the given field
getFieldErrors
public List<FieldError> getFieldErrors(String field)
Description copied from interface:ErrorsGet all errors associated with the given field.Implementations should support not only full field names like "name" but also pattern matches like "na*" or "address.*".
- Specified by:
getFieldErrorsin interfaceErrors- Parameters:
field- the field name- Returns:
- a List of
FieldErrorinstances
getFieldError
@Nullable public FieldError getFieldError(String field)
Description copied from interface:ErrorsGet the first error associated with the given field, if any.- Specified by:
getFieldErrorin interfaceErrors- Parameters:
field- the field name- Returns:
- the field-specific error, or
null
getFieldValue
@Nullable public Object getFieldValue(String field)
Description copied from interface:ErrorsReturn the current value of the given field, either the current bean property value or a rejected update from the last binding.Allows for convenient access to user-specified field values, even if there were type mismatches.
- Specified by:
getFieldValuein interfaceErrors- Parameters:
field- the field name- Returns:
- the current value of the given field
getFieldType
@Nullable public Class<?> getFieldType(String field)
Description copied from interface:ErrorsReturn the type of a given field.Implementations should be able to determine the type even when the field value is
null, for example from some associated descriptor.- Specified by:
getFieldTypein interfaceErrors- Parameters:
field- the field name- Returns:
- the type of the field, or
nullif not determinable
getTarget
@Nullable public Object getTarget()
Description copied from interface:BindingResultReturn the wrapped target object, which may be a bean, an object with public fields, a Map - depending on the concrete binding strategy.- Specified by:
getTargetin interfaceBindingResult
getModel
public Map<String,Object> getModel()
Description copied from interface:BindingResultReturn a model Map for the obtained state, exposing a BindingResult instance as 'MODEL_KEY_PREFIX+ objectName' and the object itself as 'objectName'.Note that the Map is constructed every time you're calling this method. Adding things to the map and then re-calling this method will not work.
The attributes in the model Map returned by this method are usually included in the
ModelAndViewfor a form view that uses Spring'sbindtag in a JSP, which needs access to the BindingResult instance. Spring's pre-built form controllers will do this for you when rendering a form view. When building the ModelAndView instance yourself, you need to include the attributes from the model Map returned by this method.- Specified by:
getModelin interfaceBindingResult- See Also:
Errors.getObjectName(),BindingResult.MODEL_KEY_PREFIX,ModelAndView,BindTag
getRawFieldValue
@Nullable public Object getRawFieldValue(String field)
Description copied from interface:BindingResultExtract the raw field value for the given field. Typically used for comparison purposes.- Specified by:
getRawFieldValuein interfaceBindingResult- Parameters:
field- the field to check- Returns:
- the current value of the field in its raw form, or
nullif not known
findEditor
@Nullable public PropertyEditor findEditor(@Nullable String field, @Nullable Class valueType)
Description copied from interface:BindingResultFind a custom property editor for the given type and property.- Specified by:
findEditorin interfaceBindingResult- Parameters:
field- the path of the property (name or nested path), ornullif looking for an editor for all properties of the given typevalueType- the type of the property (can benullif a property is given but should be specified in any case for consistency checking)- Returns:
- the registered editor, or
nullif none
getPropertyEditorRegistry
@Nullable public PropertyEditorRegistry getPropertyEditorRegistry()
Description copied from interface:BindingResultReturn the underlying PropertyEditorRegistry.- Specified by:
getPropertyEditorRegistryin interfaceBindingResult- Returns:
- the PropertyEditorRegistry, or
nullif none available for this BindingResult
resolveMessageCodes
public String[] resolveMessageCodes(String errorCode)
Description copied from interface:BindingResultResolve the given error code into message codes.Calls the configured
MessageCodesResolverwith appropriate parameters.- Specified by:
resolveMessageCodesin interfaceBindingResult- Parameters:
errorCode- the error code to resolve into message codes- Returns:
- the resolved message codes
resolveMessageCodes
public String[] resolveMessageCodes(String errorCode, String field)
Description copied from interface:BindingResultResolve the given error code into message codes for the given field.Calls the configured
MessageCodesResolverwith appropriate parameters.- Specified by:
resolveMessageCodesin interfaceBindingResult- Parameters:
errorCode- the error code to resolve into message codesfield- the field to resolve message codes for- Returns:
- the resolved message codes
addError
public void addError(ObjectError error)
Description copied from interface:BindingResultAdd a customObjectErrororFieldErrorto the errors list.Intended to be used by cooperating strategies such as
BindingErrorProcessor.- Specified by:
addErrorin interfaceBindingResult- See Also:
ObjectError,FieldError,BindingErrorProcessor
recordFieldValue
public void recordFieldValue(String field, Class<?> type, @Nullable Object value)
Description copied from interface:BindingResultRecord the given value for the specified field.To be used when a target object cannot be constructed, making the original field values available through
Errors.getFieldValue(java.lang.String). In case of a registered error, the rejected value will be exposed for each affected field.- Specified by:
recordFieldValuein interfaceBindingResult- Parameters:
field- the field to record the value fortype- the type of the fieldvalue- the original value
recordSuppressedField
public void recordSuppressedField(String field)
Description copied from interface:BindingResultMark the specified disallowed field as suppressed.The data binder invokes this for each field value that was detected to target a disallowed field.
- Specified by:
recordSuppressedFieldin interfaceBindingResult- See Also:
DataBinder.setAllowedFields(java.lang.String...)
getSuppressedFields
public String[] getSuppressedFields()
Description copied from interface:BindingResultReturn the list of fields that were suppressed during the bind process.Can be used to determine whether any field values were targeting disallowed fields.
- Specified by:
getSuppressedFieldsin interfaceBindingResult- See Also:
DataBinder.setAllowedFields(java.lang.String...)
getMessage
public String getMessage()
Returns diagnostic information about the errors held in this object.- Overrides:
getMessagein classThrowable