Class ResourceBundleViewResolver
- java.lang.Object
- org.springframework.context.support.ApplicationObjectSupport
- org.springframework.web.context.support.WebApplicationObjectSupport
- org.springframework.web.servlet.view.AbstractCachingViewResolver
- org.springframework.web.servlet.view.ResourceBundleViewResolver
- All Implemented Interfaces:
Aware,DisposableBean,InitializingBean,ApplicationContextAware,Ordered,ServletContextAware,ViewResolver
public class ResourceBundleViewResolver extends AbstractCachingViewResolver implements Ordered, InitializingBean, DisposableBean
AViewResolverimplementation that uses bean definitions in aResourceBundle, 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
ViewResolversupports localized view definitions, using the default support ofPropertyResourceBundle. 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
ViewResolverimplements theOrderedinterface in order to allow for flexible participation inViewResolverchaining. For example, some special views could be defined via thisViewResolver(giving it 0 as "order" value), while all remaining views could be resolved by aUrlBasedViewResolver.- Author:
- Rod Johnson, Juergen Hoeller
- See Also:
ResourceBundle.getBundle(java.lang.String),PropertyResourceBundle,UrlBasedViewResolver
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_BASENAMEThe default basename if no other basename is suppliedFields inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver
DEFAULT_CACHE_LIMIT
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description ResourceBundleViewResolver()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()Eagerly initialize Locales if necessary.voiddestroy()Close the bundle View factories on context shutdown.protected ResourceBundlegetBundle(String basename, Locale locale)Obtain the resource bundle for the given basename andLocale.protected ClassLoadergetBundleClassLoader()Return theClassLoaderto load resource bundles with.intgetOrder()Get the order value of this object.protected BeanFactoryinitFactory(Locale locale)protected ViewloadView(String viewName, Locale locale)Subclasses must implement this method, building a View object for the specified view.voidsetBasename(String basename)Set a single basename, followingResourceBundleconventions.voidsetBasenames(String... basenames)Set an array of basenames, each followingResourceBundleconventions.voidsetBundleClassLoader(ClassLoader classLoader)Set theClassLoaderto load resource bundles with.voidsetDefaultParentView(String defaultParentView)Set the default parent for views defined in theResourceBundle.voidsetLocalesToInitialize(Locale... localesToInitialize)Specify Locales to initialize eagerly, rather than lazily when actually accessed.voidsetOrder(int order)Specify the order value for this ViewResolver bean.Methods inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver
clearCache, createView, getCacheKey, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheLimit, setCacheUnresolved
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
Field Detail
DEFAULT_BASENAME
public static final String DEFAULT_BASENAME
The default basename if no other basename is supplied- See Also:
- Constant Field Values
Constructor Detail
ResourceBundleViewResolver
public ResourceBundleViewResolver()
Method Detail
setBasename
public void setBasename(String basename)
Set a single basename, followingResourceBundleconventions. The default is "views".ResourceBundlesupports different locale suffixes. For example, a base name of "views" might map toResourceBundlefiles "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.ResourceBundleusage.
setBasenames
public void setBasenames(String... basenames)
Set an array of basenames, each followingResourceBundleconventions. The default is a single basename "views".ResourceBundlesupports different locale suffixes. For example, a base name of "views" might map toResourceBundlefiles "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.ResourceBundleusage.
setBundleClassLoader
public void setBundleClassLoader(ClassLoader classLoader)
Set theClassLoaderto load resource bundles with. Default is the thread contextClassLoader.
getBundleClassLoader
protected ClassLoader getBundleClassLoader()
Return theClassLoaderto load resource bundles with.Default is the specified bundle
ClassLoader, usually the thread contextClassLoader.
setDefaultParentView
public void setDefaultParentView(String defaultParentView)
Set the default parent for views defined in theResourceBundle.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.
setLocalesToInitialize
public void setLocalesToInitialize(Locale... localesToInitialize)
Specify Locales to initialize eagerly, rather than lazily when actually accessed.Allows for pre-initialization of common Locales, eagerly checking the view configuration for those Locales.
setOrder
public void setOrder(int order)
Specify the order value for this ViewResolver bean.The default value is
Ordered.LOWEST_PRECEDENCE, meaning non-ordered.- See Also:
Ordered.getOrder()
getOrder
public int getOrder()
Description copied from interface:OrderedGet the order value of this object.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-startupvalues).Same order values will result in arbitrary sort positions for the affected objects.
- Specified by:
getOrderin interfaceOrdered- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE,Ordered.LOWEST_PRECEDENCE
afterPropertiesSet
public void afterPropertiesSet() throws BeansException
Eagerly initialize Locales if necessary.- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
BeansException- See Also:
setLocalesToInitialize(java.util.Locale...)
loadView
protected View loadView(String viewName, Locale locale) throws Exception
Description copied from class:AbstractCachingViewResolverSubclasses must implement this method, building a View object for the specified view. The returned View objects will be cached by this ViewResolver base class.Subclasses are not forced to support internationalization: A subclass that does not may simply ignore the locale parameter.
- Specified by:
loadViewin classAbstractCachingViewResolver- Parameters:
viewName- the name of the view to retrievelocale- the Locale to retrieve the view for- Returns:
- the View instance, or
nullif not found (optional, to allow for ViewResolver chaining) - Throws:
Exception- if the view couldn't be resolved- See Also:
AbstractCachingViewResolver.resolveViewName(java.lang.String, java.util.Locale)
initFactory
protected BeanFactory initFactory(Locale locale) throws BeansException
Initialize the ViewBeanFactoryfrom theResourceBundle, for the givenlocale.Synchronized because of access by parallel threads.
- Parameters:
locale- the targetLocale- Returns:
- the View factory for the given Locale
- Throws:
BeansException- in case of initialization errors
getBundle
protected ResourceBundle getBundle(String basename, Locale locale) throws MissingResourceException
Obtain the resource bundle for the given basename andLocale.- Parameters:
basename- the basename to look forlocale- theLocaleto look for- Returns:
- the corresponding
ResourceBundle - Throws:
MissingResourceException- if no matching bundle could be found- See Also:
ResourceBundle.getBundle(String, Locale, ClassLoader)
destroy
public void destroy() throws BeansException
Close the bundle View factories on context shutdown.- Specified by:
destroyin interfaceDisposableBean- Throws:
BeansException