Top Description Fields Constructors Methods
org.springframework.context.support

public Class MessageSourceAccessor

extends Object
Class Inheritance
Imports
java.util.Locale, org.springframework.context.MessageSource, .MessageSourceResolvable, .NoSuchMessageException, org.springframework.context.i18n.LocaleContextHolder, org.springframework.lang.Nullable

Helper class for easy access to messages from a MessageSource, providing various overloaded getMessage methods.

Available from ApplicationObjectSupport, but also reusable as a standalone helper to delegate to in application objects.

Author
Juergen Hoeller
Since
23.10.2003
See Also
ApplicationObjectSupport#getMessageSourceAccessor

Field Summary

Modifier and TypeField and Description
private final Locale
private final MessageSource

Constructor Summary

AccessConstructor and Description
public
MessageSourceAccessor(MessageSource
the MessageSource to wrap
messageSource
)

Create a new MessageSourceAccessor, using LocaleContextHolder's locale as default locale.

public
MessageSourceAccessor(MessageSource
the MessageSource to wrap
messageSource
,
Locale
the default locale to use for message access
defaultLocale
)

Create a new MessageSourceAccessor, using the given default locale.

Method Summary

Modifier and TypeMethod and Description
protected Locale
getDefaultLocale()

Return the default locale to use if no explicit locale has been given.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code and the default Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
String
the String to return if the lookup fails
defaultMessage
,
Locale
the Locale in which to do lookup
locale
)

Retrieve the message for the given code and the given Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
)

Retrieve the message for the given code and the default Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
String
the String to return if the lookup fails
defaultMessage
,
Locale
the Locale in which to do lookup
locale
)

Retrieve the message for the given code and the given Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
)

Retrieve the message for the given code and the default Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Locale
the Locale in which to do lookup
locale
)

Retrieve the message for the given code and the given Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
)

Retrieve the message for the given code and the default Locale.

public String

Returns:

the message
getMessage
(String
the code of the message
code
,
Object[]
arguments for the message, or null if none
args
,
Locale
the Locale in which to do lookup
locale
)

Retrieve the message for the given code and the given Locale.

public String

Returns:

the message
getMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
)

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) in the default Locale.

public String

Returns:

the message
getMessage
(MessageSourceResolvable
the MessageSourceResolvable
resolvable
,
Locale
the Locale in which to do lookup
locale
)

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) in the given Locale.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultLocaleback to summary
private final Locale defaultLocale
Annotations
@Nullable
messageSourceback to summary
private final MessageSource messageSource

Constructor Detail

MessageSourceAccessorback to summary
public MessageSourceAccessor(MessageSource messageSource)

Create a new MessageSourceAccessor, using LocaleContextHolder's locale as default locale.

Parameters
messageSource:MessageSource

the MessageSource to wrap

See Also
org.springframework.context.i18n.LocaleContextHolder#getLocale()
MessageSourceAccessorback to summary
public MessageSourceAccessor(MessageSource messageSource, Locale defaultLocale)

Create a new MessageSourceAccessor, using the given default locale.

Parameters
messageSource:MessageSource

the MessageSource to wrap

defaultLocale:Locale

the default locale to use for message access

Method Detail

getDefaultLocaleback to summary
protected Locale getDefaultLocale()

Return the default locale to use if no explicit locale has been given.

The default implementation returns the default locale passed into the corresponding constructor, or LocaleContextHolder's locale as fallback. Can be overridden in subclasses.

See Also
MessageSourceAccessor(org.springframework.context.MessageSource, java.util.Locale), org.springframework.context.i18n.LocaleContextHolder#getLocale()
getMessageback to summary
public String getMessage(String code, String defaultMessage)

Retrieve the message for the given code and the default Locale.

Parameters
code:String

the code of the message

defaultMessage:String

the String to return if the lookup fails

Returns:String

the message

getMessageback to summary
public String getMessage(String code, String defaultMessage, Locale locale)

Retrieve the message for the given code and the given Locale.

Parameters
code:String

the code of the message

defaultMessage:String

the String to return if the lookup fails

locale:Locale

the Locale in which to do lookup

Returns:String

the message

getMessageback to summary
public String getMessage(String code, Object[] args, String defaultMessage)

Retrieve the message for the given code and the default Locale.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

defaultMessage:String

the String to return if the lookup fails

Returns:String

the message

getMessageback to summary
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale)

Retrieve the message for the given code and the given Locale.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

defaultMessage:String

the String to return if the lookup fails

locale:Locale

the Locale in which to do lookup

Returns:String

the message

getMessageback to summary
public String getMessage(String code) throws NoSuchMessageException

Retrieve the message for the given code and the default Locale.

Parameters
code:String

the code of the message

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(String code, Locale locale) throws NoSuchMessageException

Retrieve the message for the given code and the given Locale.

Parameters
code:String

the code of the message

locale:Locale

the Locale in which to do lookup

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(String code, Object[] args) throws NoSuchMessageException

Retrieve the message for the given code and the default Locale.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException

Retrieve the message for the given code and the given Locale.

Parameters
code:String

the code of the message

args:Object[]

arguments for the message, or null if none

locale:Locale

the Locale in which to do lookup

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) in the default Locale.

Parameters
resolvable:MessageSourceResolvable

the MessageSourceResolvable

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found
getMessageback to summary
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException

Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) in the given Locale.

Parameters
resolvable:MessageSourceResolvable

the MessageSourceResolvable

locale:Locale

the Locale in which to do lookup

Returns:String

the message

Exceptions
NoSuchMessageException:
if not found