public class ResourceBundleViewResolver extends AbstractCachingViewResolver implements Ordered, InitializingBean, DisposableBean
ViewResolver
implementation that uses
bean definitions in a ResourceBundle
, specified by the bundle basename.
The bundle is typically defined in a properties file, located in the classpath. The default bundle basename is "views".
This ViewResolver
supports localized view definitions, using the
default support of PropertyResourceBundle
. For example, the
basename "views" will be resolved as class path resources "views_de_AT.properties",
"views_de.properties", "views.properties" - for a given Locale "de_AT".
Note: This ViewResolver
implements the Ordered
interface
in order to allow for flexible participation in ViewResolver
chaining.
For example, some special views could be defined via this ViewResolver
(giving it 0 as "order" value), while all remaining views could be resolved by
a UrlBasedViewResolver
.
ResourceBundle.getBundle(java.lang.String)
,
PropertyResourceBundle
,
UrlBasedViewResolver
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_BASENAME
The default basename if no other basename is supplied.
|
DEFAULT_CACHE_LIMIT
logger
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
ResourceBundleViewResolver() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Eagerly initialize Locales if necessary.
|
void |
destroy()
Close the bundle View factories on context shutdown.
|
protected java.util.ResourceBundle |
getBundle(java.lang.String basename,
java.util.Locale locale)
Obtain the resource bundle for the given basename and
Locale . |
protected java.lang.ClassLoader |
getBundleClassLoader()
Return the
ClassLoader to load resource bundles with. |
int |
getOrder()
Get the order value of this object.
|
protected BeanFactory |
initFactory(java.util.Locale locale)
|
protected View |
loadView(java.lang.String viewName,
java.util.Locale locale)
Subclasses must implement this method, building a View object
for the specified view.
|
void |
setBasename(java.lang.String basename)
Set a single basename, following
ResourceBundle conventions. |
void |
setBasenames(java.lang.String... basenames)
Set an array of basenames, each following
ResourceBundle
conventions. |
void |
setBundleClassLoader(java.lang.ClassLoader classLoader)
Set the
ClassLoader to load resource bundles with. |
void |
setDefaultParentView(java.lang.String defaultParentView)
Set the default parent for views defined in the
ResourceBundle . |
void |
setLocalesToInitialize(java.util.Locale... localesToInitialize)
Specify Locales to initialize eagerly, rather than lazily when actually accessed.
|
void |
setOrder(int order) |
clearCache, createView, getCacheKey, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheLimit, setCacheUnresolved
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
public static final java.lang.String DEFAULT_BASENAME
public void setOrder(int order)
public int getOrder()
Ordered
Higher values are interpreted as lower priority. As a consequence,
the object with the lowest value has the highest priority (somewhat
analogous to Servlet load-on-startup
values).
Same order values will result in arbitrary sort positions for the affected objects.
getOrder
in interface Ordered
Ordered.HIGHEST_PRECEDENCE
,
Ordered.LOWEST_PRECEDENCE
public void setBasename(java.lang.String basename)
ResourceBundle
conventions.
The default is "views".
ResourceBundle
supports different suffixes. For example,
a base name of "views" might map to ResourceBundle
files
"views", "views_en_au" and "views_de".
Note that ResourceBundle names are effectively classpath locations: As a
consequence, the JDK's standard ResourceBundle treats dots as package separators.
This means that "test.theme" is effectively equivalent to "test/theme",
just like it is for programmatic java.util.ResourceBundle
usage.
setBasenames(java.lang.String...)
,
ResourceBundle.getBundle(String)
public void setBasenames(java.lang.String... basenames)
ResourceBundle
conventions. The default is a single basename "views".
ResourceBundle
supports different suffixes. For example,
a base name of "views" might map to ResourceBundle
files
"views", "views_en_au" and "views_de".
The associated resource bundles will be checked sequentially when resolving a message code. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.
Note that ResourceBundle names are effectively classpath locations: As a
consequence, the JDK's standard ResourceBundle treats dots as package separators.
This means that "test.theme" is effectively equivalent to "test/theme",
just like it is for programmatic java.util.ResourceBundle
usage.
setBasename(java.lang.String)
,
ResourceBundle.getBundle(String)
public void setBundleClassLoader(java.lang.ClassLoader classLoader)
ClassLoader
to load resource bundles with.
Default is the thread context ClassLoader
.protected java.lang.ClassLoader getBundleClassLoader()
ClassLoader
to load resource bundles with.
Default is the specified bundle ClassLoader
,
usually the thread context ClassLoader
.
public void setDefaultParentView(java.lang.String defaultParentView)
ResourceBundle
.
This avoids repeated "yyy1.(parent)=xxx", "yyy2.(parent)=xxx" definitions in the bundle, especially if all defined views share the same parent.
The parent will typically define the view class and common attributes. Concrete views might simply consist of an URL definition then: a la "yyy1.url=/my.jsp", "yyy2.url=/your.jsp".
View definitions that define their own parent or carry their own class can still override this. Strictly speaking, the rule that a default parent setting does not apply to a bean definition that carries a class is there for backwards compatibility reasons. It still matches the typical use case.
public void setLocalesToInitialize(java.util.Locale... localesToInitialize)
Allows for pre-initialization of common Locales, eagerly checking the view configuration for those Locales.
public void afterPropertiesSet() throws BeansException
afterPropertiesSet
in interface InitializingBean
BeansException
setLocalesToInitialize(java.util.Locale...)
protected View loadView(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
AbstractCachingViewResolver
Subclasses are not forced to support internationalization: A subclass that does not may simply ignore the locale parameter.
loadView
in class AbstractCachingViewResolver
viewName
- the name of the view to retrievelocale
- the Locale to retrieve the view fornull
if not found
(optional, to allow for ViewResolver chaining)java.lang.Exception
- if the view couldn't be resolvedAbstractCachingViewResolver.resolveViewName(java.lang.String, java.util.Locale)
protected BeanFactory initFactory(java.util.Locale locale) throws BeansException
BeanFactory
from the ResourceBundle
,
for the given locale
.
Synchronized because of access by parallel threads.
locale
- the target Locale
BeansException
- in case of initialization errorsprotected java.util.ResourceBundle getBundle(java.lang.String basename, java.util.Locale locale) throws java.util.MissingResourceException
Locale
.basename
- the basename to look forlocale
- the Locale
to look forResourceBundle
java.util.MissingResourceException
- if no matching bundle could be foundResourceBundle.getBundle(String, java.util.Locale, ClassLoader)
public void destroy() throws BeansException
destroy
in interface DisposableBean
BeansException