Top Description Inners Fields Constructors Methods
org.springframework.validation

public Class DefaultMessageCodesResolver

extends Object
implements MessageCodesResolver, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.springframework.validation.MessageCodesResolver
Annotations
@SuppressWarnings:serial
Imports
java.io.Serializable, java.util.ArrayList, .Collection, .LinkedHashSet, .List, .Set, .StringJoiner, org.springframework.lang.Nullable, org.springframework.util.StringUtils

Default implementation of the MessageCodesResolver interface.

Will create two message codes for an object error, in the following order (when using the prefixed formatter):

Will create four message codes for a field specification, in the following order:

For example, in case of code "typeMismatch", object name "user", field "age":

This resolution algorithm thus can be leveraged for example to show specific messages for binding errors like "required" and "typeMismatch":

In case of array, List or java.util.Map properties, both codes for specific elements and for the whole collection are generated. Assuming a field "name" of an array "groups" in object "user":

By default the errorCodes will be placed at the beginning of constructed message strings. The messageCodeFormatter property can be used to specify an alternative concatenation format.

In order to group all codes into a specific category within your resource bundles, e.g. "validation.typeMismatch.name" instead of the default "typeMismatch.name", consider specifying a prefix to be applied.

Authors
Juergen Hoeller, Phillip Webb, Chris Beams
Since
1.0.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
DefaultMessageCodesResolver.Format

Common message code formats.

Field Summary

Modifier and TypeField and Description
public static final String
CODE_SEPARATOR

The separator that this implementation uses when resolving message codes.

private static final MessageCodeFormatter
private MessageCodeFormatter
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
addCode(Collection<String> codeList, String errorCode, String objectName, String field)

private void
addCodes(Collection<String> codeList, String errorCode, String objectName, Iterable<String> fields)

protected void
buildFieldList(String field, List<String> fieldList)

Add both keyed and non-keyed entries for the supplied field to the supplied field list.

protected String
getPrefix()

Return the prefix to be applied to any code built by this resolver.

protected String

Returns:

the final message code to be returned
postProcessMessageCode
(String
the message code as built by this resolver
code
)

Post-process the given message code, built by this resolver.

public String[]
resolveMessageCodes(String
the error code used for rejecting the object
errorCode
,
String
the name of the object
objectName
)

Implements org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

Build message codes for the given error code and object name.

public String[]

Returns:

the list of codes
resolveMessageCodes
(String
the error code used for rejecting the value
errorCode
,
String
the name of the object
objectName
,
String
the field name
field
,
Class<?>
the field type (may be null if not determinable)
fieldType
)

Implements org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

Build the code list for the given code and field: an object/field-specific code, a field-specific code, a plain error code.

public void
setMessageCodeFormatter(MessageCodeFormatter formatter)

Specify the format for message codes built by this resolver.

public void
setPrefix(String prefix)

Specify a prefix to be applied to any code built by this resolver.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CODE_SEPARATORback to summary
public static final String CODE_SEPARATOR

The separator that this implementation uses when resolving message codes.

DEFAULT_FORMATTERback to summary
private static final MessageCodeFormatter DEFAULT_FORMATTER
formatterback to summary
private MessageCodeFormatter formatter
prefixback to summary
private String prefix

Constructor Detail

DefaultMessageCodesResolverback to summary
public DefaultMessageCodesResolver()

Method Detail

addCodeback to summary
private void addCode(Collection<String> codeList, String errorCode, String objectName, String field)
addCodesback to summary
private void addCodes(Collection<String> codeList, String errorCode, String objectName, Iterable<String> fields)
buildFieldListback to summary
protected void buildFieldList(String field, List<String> fieldList)

Add both keyed and non-keyed entries for the supplied field to the supplied field list.

getPrefixback to summary
protected String getPrefix()

Return the prefix to be applied to any code built by this resolver.

Returns an empty String in case of no prefix.

postProcessMessageCodeback to summary
protected String postProcessMessageCode(String code)

Post-process the given message code, built by this resolver.

The default implementation applies the specified prefix, if any.

Parameters
code:String

the message code as built by this resolver

Returns:String

the final message code to be returned

See Also
setPrefix
resolveMessageCodesback to summary
public String[] resolveMessageCodes(String errorCode, String objectName)

Implements org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

Doc from org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

Build message codes for the given error code and object name. Used for building the codes list of an ObjectError.

Parameters
errorCode:String

the error code used for rejecting the object

objectName:String

the name of the object

Returns:String[]

the message codes to use

Annotations
@Override
resolveMessageCodesback to summary
public String[] resolveMessageCodes(String errorCode, String objectName, String field, Class<?> fieldType)

Implements org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

Build the code list for the given code and field: an object/field-specific code, a field-specific code, a plain error code.

Arrays, Lists and Maps are resolved both for specific elements and the whole collection.

See the class level javadoc for details on the generated codes.

Parameters
errorCode:String

Doc from org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

the error code used for rejecting the value

objectName:String

Doc from org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

the name of the object

field:String

Doc from org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

the field name

fieldType:Class<?>

Doc from org.springframework.validation.MessageCodesResolver.resolveMessageCodes.

the field type (may be null if not determinable)

Returns:String[]

the list of codes

Annotations
@Override
setMessageCodeFormatterback to summary
public void setMessageCodeFormatter(MessageCodeFormatter formatter)

Specify the format for message codes built by this resolver.

The default is Format#PREFIX_ERROR_CODE.

Since
3.2
See Also
Format
setPrefixback to summary
public void setPrefix(String prefix)

Specify a prefix to be applied to any code built by this resolver.

Default is none. Specify, for example, "validation." to get error codes like "validation.typeMismatch.name".

org.springframework.validation back to summary

public sealed Enum DefaultMessageCodesResolver.Format

extends Enum<DefaultMessageCodesResolver.Format>
implements MessageCodeFormatter
Class Inheritance
All Implemented Interfaces
org.springframework.validation.MessageCodeFormatter

Common message code formats.
See Also
MessageCodeFormatter, DefaultMessageCodesResolver#setMessageCodeFormatter(MessageCodeFormatter)

Field Summary

Modifier and TypeField and Description
public static final DefaultMessageCodesResolver.Format
POSTFIX_ERROR_CODE

Postfix the error code at the end of the generated message code.

public static final DefaultMessageCodesResolver.Format
PREFIX_ERROR_CODE

Prefix the error code at the beginning of the generated message code.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String
toDelimitedString(String... elements)

Concatenate the given elements, delimiting each with DefaultMessageCodesResolver#CODE_SEPARATOR, skipping zero-length or null elements altogether.

public static DefaultMessageCodesResolver.Format
public static DefaultMessageCodesResolver.Format[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

POSTFIX_ERROR_CODEback to summary
public static final DefaultMessageCodesResolver.Format POSTFIX_ERROR_CODE

Postfix the error code at the end of the generated message code. e.g.: object name + "." + field + "." + errorCode

PREFIX_ERROR_CODEback to summary
public static final DefaultMessageCodesResolver.Format PREFIX_ERROR_CODE

Prefix the error code at the beginning of the generated message code. e.g.: errorCode + "." + object name + "." + field

Constructor Detail

Formatback to summary
private Format()

Method Detail

toDelimitedStringback to summary
public static String toDelimitedString(String... elements)

Concatenate the given elements, delimiting each with DefaultMessageCodesResolver#CODE_SEPARATOR, skipping zero-length or null elements altogether.

valueOfback to summary
public static DefaultMessageCodesResolver.Format valueOf(String name)
valuesback to summary
public static DefaultMessageCodesResolver.Format[] values()