Package org.apache.struts2.validator
Class DelegatingValidatorContext
java.lang.Object
org.apache.struts2.validator.DelegatingValidatorContext
- All Implemented Interfaces:
ValidationAware
,LocaleProvider
,TextProvider
,ValidatorContext
- Direct Known Subclasses:
VisitorFieldValidator.AppendingValidatorContext
A default implementation of the
ValidatorContext
interface.- Author:
- Jason Carreira, Rainer Hermanns
-
Constructor Summary
ConstructorDescriptionDelegatingValidatorContext
(Object object, TextProviderFactory textProviderFactory) Creates a new validation context given an object - usually an Action.DelegatingValidatorContext
(ValidationAware validationAware, TextProvider textProvider, LocaleProvider localeProvider) Creates a new validation context given a ValidationAware object, and a text and locale provider. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addActionError
(String anErrorMessage) Add an Action-level error message to this Action.void
addActionMessage
(String aMessage) Add an Action-level message to this Action.void
addFieldError
(String fieldName, String errorMessage) Add an error message for a given field.Get the Collection of Action-level error messages for this action.Get the Collection of Action-level messages for this action.Get the field specific errors associated with this action.getFullFieldName
(String fieldName) Translates a simple field name into a full field name in OGNL syntax.Gets the provided locale.Gets a message based on a message key or if no message is found the provided key is returned.Gets a message based on a key, or, if the message is not found, a supplied default value is returned.Gets a message based on a key using the supplied args, as defined inMessageFormat
, or the provided key if no message is found.Gets a message based on a key using the supplied obj, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.getText
(String key, String defaultValue, String[] args, ValueStack stack) Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.getText
(String key, String defaultValue, List<?> args, ValueStack stack) Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.Gets a message based on a key using the supplied args, as defined inMessageFormat
or the provided key if no message is found.protected TextProvider
getTexts()
Get the resource bundle associated with the implementing class (usually an action).Get the named bundle, such as "com/acme/Foo".protected ValidationAware
boolean
Check whether there are any Action-level error messages.boolean
Checks whether there are any Action-level messages.boolean
Checks whether there are any action errors or field errors.boolean
Check whether there are any field errors associated with this action.boolean
Checks if a message key exists.boolean
isValidLocale
(Locale locale) Validates if providedLocale
is valueboolean
isValidLocaleString
(String localeStr) Validates if provided string is a validLocale
protected static LocaleProvider
makeLocaleProvider
(Object object) makeTextProvider
(Object object, TextProviderFactory textProviderFactory) protected static ValidationAware
makeValidationAware
(Object object) void
setActionErrors
(Collection<String> errorMessages) Set the Collection of Action-level String error messages.void
setActionMessages
(Collection<String> messages) Set the Collection of Action-level String messages (not errors).void
setFieldErrors
(Map<String, List<String>> errorMap) Set the field error map of fieldname (String) to Collection of String error messages.protected void
setTextProvider
(TextProvider textProvider) protected void
setValidationAware
(ValidationAware validationAware) Tries to convert provided locale string intoLocale
or returns null
-
Constructor Details
-
DelegatingValidatorContext
public DelegatingValidatorContext(ValidationAware validationAware, TextProvider textProvider, LocaleProvider localeProvider) Creates a new validation context given a ValidationAware object, and a text and locale provider. These objects are used internally to set errors and get and set error text.- Parameters:
validationAware
- a validation aware objecttextProvider
- a text providerlocaleProvider
- a local provider
-
DelegatingValidatorContext
Creates a new validation context given an object - usually an Action. The internal objects (validation aware instance and a locale and text provider) are created based on the given action.- Parameters:
object
- the object to use for validation (usually an Action).
-
-
Method Details
-
setActionErrors
Description copied from interface:ValidationAware
Set the Collection of Action-level String error messages.- Specified by:
setActionErrors
in interfaceValidationAware
- Parameters:
errorMessages
- Collection of String error messages
-
getActionErrors
Description copied from interface:ValidationAware
Get the Collection of Action-level error messages for this action. Error messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection.- Specified by:
getActionErrors
in interfaceValidationAware
- Returns:
- Collection of String error messages
-
setActionMessages
Description copied from interface:ValidationAware
Set the Collection of Action-level String messages (not errors).- Specified by:
setActionMessages
in interfaceValidationAware
- Parameters:
messages
- Collection of String messages (not errors).
-
getActionMessages
Description copied from interface:ValidationAware
Get the Collection of Action-level messages for this action. Messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection.- Specified by:
getActionMessages
in interfaceValidationAware
- Returns:
- Collection of String messages
-
setFieldErrors
Description copied from interface:ValidationAware
Set the field error map of fieldname (String) to Collection of String error messages.- Specified by:
setFieldErrors
in interfaceValidationAware
- Parameters:
errorMap
- field error map
-
getFieldErrors
Description copied from interface:ValidationAware
Get the field specific errors associated with this action. Error messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection.- Specified by:
getFieldErrors
in interfaceValidationAware
- Returns:
- Map with errors mapped from fieldname (String) to Collection of String error messages
-
getFullFieldName
Description copied from interface:ValidatorContext
Translates a simple field name into a full field name in OGNL syntax.- Specified by:
getFullFieldName
in interfaceValidatorContext
- Parameters:
fieldName
- the field name to lookup.- Returns:
- the full field name in OGNL syntax.
-
getLocale
Description copied from interface:LocaleProvider
Gets the provided locale.- Specified by:
getLocale
in interfaceLocaleProvider
- Returns:
- the locale.
-
isValidLocaleString
Description copied from interface:LocaleProvider
Validates if provided string is a validLocale
- Specified by:
isValidLocaleString
in interfaceLocaleProvider
- Parameters:
localeStr
- a String representing locale, e.g. en_EN- Returns:
- true if valid
-
isValidLocale
Description copied from interface:LocaleProvider
Validates if providedLocale
is value- Specified by:
isValidLocale
in interfaceLocaleProvider
- Parameters:
locale
- instance ofLocale
to validate- Returns:
- true if valid
-
toLocale
Description copied from interface:LocaleProvider
Tries to convert provided locale string intoLocale
or returns null- Specified by:
toLocale
in interfaceLocaleProvider
- Parameters:
localeStr
- a String representing locale, e.g.: en_EN- Returns:
- instance of
Locale
or null
-
hasKey
Description copied from interface:TextProvider
Checks if a message key exists.- Specified by:
hasKey
in interfaceTextProvider
- Parameters:
key
- message key to check for- Returns:
- boolean true if key exists, false otherwise.
-
getText
Description copied from interface:TextProvider
Gets a message based on a message key or if no message is found the provided key is returned.- Specified by:
getText
in interfaceTextProvider
- Parameters:
aTextName
- the resource bundle key that is to be searched for- Returns:
- the message as found in the resource bundle, or the provided key if none is found.
-
getText
Description copied from interface:TextProvider
Gets a message based on a key, or, if the message is not found, a supplied default value is returned.- Specified by:
getText
in interfaceTextProvider
- Parameters:
aTextName
- the resource bundle key that is to be searched fordefaultValue
- the default value which will be returned if no message is found- Returns:
- the message as found in the resource bundle, or defaultValue if none is found
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied obj, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.- Specified by:
getText
in interfaceTextProvider
- Parameters:
aTextName
- the resource bundle key that is to be searched fordefaultValue
- the default value which will be returned if no message is foundobj
- obj to be used in aMessageFormat
message- Returns:
- the message as found in the resource bundle, or defaultValue if none is found
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied args, as defined inMessageFormat
or the provided key if no message is found.- Specified by:
getText
in interfaceTextProvider
- Parameters:
aTextName
- the resource bundle key that is to be searched forargs
- a list args to be used in aMessageFormat
message- Returns:
- the message as found in the resource bundle, or the provided key if none is found.
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied args, as defined inMessageFormat
, or the provided key if no message is found.- Specified by:
getText
in interfaceTextProvider
- Parameters:
key
- the resource bundle key that is to be searched forargs
- an array args to be used in aMessageFormat
message- Returns:
- the message as found in the resource bundle, or the provided key if none is found.
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.- Specified by:
getText
in interfaceTextProvider
- Parameters:
aTextName
- the resource bundle key that is to be searched fordefaultValue
- the default value which will be returned if no message is foundargs
- a list args to be used in aMessageFormat
message- Returns:
- the message as found in the resource bundle, or defaultValue if none is found
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned.- Specified by:
getText
in interfaceTextProvider
- Parameters:
key
- the resource bundle key that is to be searched fordefaultValue
- the default value which will be returned if no message is foundargs
- an array args to be used in aMessageFormat
message- Returns:
- the message as found in the resource bundle, or defaultValue if none is found
-
getTexts
Description copied from interface:TextProvider
Get the named bundle, such as "com/acme/Foo".- Specified by:
getTexts
in interfaceTextProvider
- Parameters:
aBundleName
- the name of the resource bundle, such as"com/acme/Foo"
.- Returns:
- the bundle
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned. Instead of using the value stack in the ActionContext this version of the getText() method uses the provided value stack.- Specified by:
getText
in interfaceTextProvider
- Parameters:
key
- the resource bundle key that is to be searched fordefaultValue
- the default value which will be returned if no message is foundargs
- a list args to be used in aMessageFormat
messagestack
- the value stack to use for finding the text- Returns:
- the message as found in the resource bundle, or defaultValue if none is found
-
getText
Description copied from interface:TextProvider
Gets a message based on a key using the supplied args, as defined inMessageFormat
, or, if the message is not found, a supplied default value is returned. Instead of using the value stack in the ActionContext this version of the getText() method uses the provided value stack.- Specified by:
getText
in interfaceTextProvider
- Parameters:
key
- the resource bundle key that is to be searched fordefaultValue
- the default value which will be returned if no message is foundargs
- an array args to be used in aMessageFormat
messagestack
- the value stack to use for finding the text- Returns:
- the message as found in the resource bundle, or defaultValue if none is found
-
getTexts
Description copied from interface:TextProvider
Get the resource bundle associated with the implementing class (usually an action).- Specified by:
getTexts
in interfaceTextProvider
- Returns:
- the bundle
-
addActionError
Description copied from interface:ValidationAware
Add an Action-level error message to this Action.- Specified by:
addActionError
in interfaceValidationAware
- Parameters:
anErrorMessage
- the error message
-
addActionMessage
Description copied from interface:ValidationAware
Add an Action-level message to this Action.- Specified by:
addActionMessage
in interfaceValidationAware
- Parameters:
aMessage
- the message
-
addFieldError
Description copied from interface:ValidationAware
Add an error message for a given field.- Specified by:
addFieldError
in interfaceValidationAware
- Parameters:
fieldName
- name of fielderrorMessage
- the error message
-
hasActionErrors
public boolean hasActionErrors()Description copied from interface:ValidationAware
Check whether there are any Action-level error messages.- Specified by:
hasActionErrors
in interfaceValidationAware
- Returns:
- true if any Action-level error messages have been registered
-
hasActionMessages
public boolean hasActionMessages()Description copied from interface:ValidationAware
Checks whether there are any Action-level messages.- Specified by:
hasActionMessages
in interfaceValidationAware
- Returns:
- true if any Action-level messages have been registered
-
hasErrors
public boolean hasErrors()Description copied from interface:ValidationAware
Checks whether there are any action errors or field errors.- Specified by:
hasErrors
in interfaceValidationAware
- Returns:
(hasActionErrors() || hasFieldErrors())
-
hasFieldErrors
public boolean hasFieldErrors()Description copied from interface:ValidationAware
Check whether there are any field errors associated with this action.- Specified by:
hasFieldErrors
in interfaceValidationAware
- Returns:
- whether there are any field errors
-
makeTextProvider
-
makeLocaleProvider
-
makeValidationAware
-
setTextProvider
-
getTextProvider
-
setValidationAware
-
getValidationAware
-