public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver
ViewResolver
implementations. Caches View
objects
once resolved: This means that view resolution won't be a performance problem,
no matter how costly initial view retrieval is.
Subclasses need to implement the loadView(java.lang.String, java.util.Locale)
template method,
building the View object for a specific view name and locale.
loadView(java.lang.String, java.util.Locale)
Modifier and Type | Field and Description |
---|---|
private int |
cacheLimit
The maximum number of entries in the cache
|
private boolean |
cacheUnresolved
Whether we should refrain from resolving views again if unresolved once
|
static int |
DEFAULT_CACHE_LIMIT
Default maximum number of entries for the view cache: 1024
|
private static View |
UNRESOLVED_VIEW
Dummy marker object for unresolved views in the cache Maps
|
private java.util.Map<java.lang.Object,View> |
viewAccessCache
Fast access cache for Views, returning already cached instances without a global lock
|
private java.util.Map<java.lang.Object,View> |
viewCreationCache
Map from view key to View instance, synchronized for View creation
|
logger
Constructor and Description |
---|
AbstractCachingViewResolver() |
Modifier and Type | Method and Description |
---|---|
void |
clearCache()
Clear the entire view cache, removing all cached view objects.
|
protected View |
createView(java.lang.String viewName,
java.util.Locale locale)
Create the actual View object.
|
protected java.lang.Object |
getCacheKey(java.lang.String viewName,
java.util.Locale locale)
Return the cache key for the given view name and the given locale.
|
int |
getCacheLimit()
Return the maximum number of entries for the view cache.
|
boolean |
isCache()
Return if caching is enabled.
|
boolean |
isCacheUnresolved()
Return if caching of unresolved views is enabled.
|
protected abstract View |
loadView(java.lang.String viewName,
java.util.Locale locale)
Subclasses must implement this method, building a View object
for the specified view.
|
void |
removeFromCache(java.lang.String viewName,
java.util.Locale locale)
Provides functionality to clear the cache for a certain view.
|
View |
resolveViewName(java.lang.String viewName,
java.util.Locale locale)
Resolve the given view by name.
|
void |
setCache(boolean cache)
Enable or disable caching.
|
void |
setCacheLimit(int cacheLimit)
Specify the maximum number of entries for the view cache.
|
void |
setCacheUnresolved(boolean cacheUnresolved)
Whether a view name once resolved to
null should be cached and
automatically resolved to null subsequently. |
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
public static final int DEFAULT_CACHE_LIMIT
private static final View UNRESOLVED_VIEW
private volatile int cacheLimit
private boolean cacheUnresolved
private final java.util.Map<java.lang.Object,View> viewAccessCache
private final java.util.Map<java.lang.Object,View> viewCreationCache
public void setCacheLimit(int cacheLimit)
public int getCacheLimit()
public void setCache(boolean cache)
This is equivalent to setting the "cacheLimit"
property to the default limit (1024) or to 0, respectively.
Default is "true": caching is enabled. Disable this only for debugging and development.
public boolean isCache()
public void setCacheUnresolved(boolean cacheUnresolved)
null
should be cached and
automatically resolved to null
subsequently.
Default is "true": unresolved view names are being cached, as of Spring 3.1.
Note that this flag only applies if the general "cache"
flag is kept at its default of "true" as well.
Of specific interest is the ability for some AbstractUrlBasedView
implementations (FreeMarker, Tiles) to check if an underlying resource
exists via AbstractUrlBasedView.checkResource(Locale)
.
With this flag set to "false", an underlying resource that re-appears
is noticed and used. With the flag set to "true", one check is made only.
public boolean isCacheUnresolved()
public View resolveViewName(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
ViewResolver
Note: To allow for ViewResolver chaining, a ViewResolver should
return null
if a view with the given name is not defined in it.
However, this is not required: Some ViewResolvers will always attempt
to build View objects with the given name, unable to return null
(rather throwing an exception when View creation failed).
resolveViewName
in interface ViewResolver
viewName
- name of the view to resolvelocale
- Locale in which to resolve the view.
ViewResolvers that support internationalization should respect this.null
if not found
(optional, to allow for ViewResolver chaining)java.lang.Exception
- if the view cannot be resolved
(typically in case of problems creating an actual View object)protected java.lang.Object getCacheKey(java.lang.String viewName, java.util.Locale locale)
Default is a String consisting of view name and locale suffix. Can be overridden in subclasses.
Needs to respect the locale in general, as a different locale can lead to a different view resource.
public void removeFromCache(java.lang.String viewName, java.util.Locale locale)
This can be handy in case developer are able to modify views (e.g. FreeMarker templates) at runtime after which you'd need to clear the cache for the specified view.
viewName
- the view name for which the cached view object
(if any) needs to be removedlocale
- the locale for which the view object should be removedpublic void clearCache()
protected View createView(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
The default implementation delegates to loadView(java.lang.String, java.util.Locale)
.
This can be overridden to resolve certain view names in a special fashion,
before delegating to the actual loadView
implementation
provided by the subclass.
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 resolvedloadView(java.lang.String, java.util.Locale)
protected abstract View loadView(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
Subclasses are not forced to support internationalization: A subclass that does not may simply ignore the locale parameter.
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 resolvedresolveViewName(java.lang.String, java.util.Locale)