Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.view

public abstract Class AbstractCachingViewResolver

extends WebApplicationObjectSupport
implements ViewResolver
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.ViewResolver
Known Direct Subclasses
org.springframework.web.servlet.view.ResourceBundleViewResolver, org.springframework.web.servlet.view.UrlBasedViewResolver, org.springframework.web.servlet.view.XmlViewResolver
Imports
java.util.LinkedHashMap, .Locale, .Map, java.util.concurrent.ConcurrentHashMap, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.context.support.WebApplicationObjectSupport, org.springframework.web.servlet.View, .ViewResolver

Convenient base class for org.springframework.web.servlet.ViewResolver implementations. Caches org.springframework.web.servlet.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 template method, building the View object for a specific view name and locale.

Authors
Rod Johnson, Juergen Hoeller
See Also
loadView

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
AbstractCachingViewResolver.CacheFilter

Filter that determines if view should be cached.

Field Summary

Modifier and TypeField and Description
private AbstractCachingViewResolver.CacheFilter
cacheFilter

Filter function that determines if view should be cached.

private volatile int
cacheLimit

The maximum number of entries in the cache.

private boolean
cacheUnresolved

Whether we should refrain from resolving views again if unresolved once.

private static final AbstractCachingViewResolver.CacheFilter
DEFAULT_CACHE_FILTER

Default cache filter that always caches.

public static final int
DEFAULT_CACHE_LIMIT

Default maximum number of entries for the view cache: 1024.

private static final View
UNRESOLVED_VIEW

Dummy marker object for unresolved views in the cache Maps.

private final Map<Object, View>
viewAccessCache

Fast access cache for Views, returning already cached instances without a global lock.

private final Map<Object, View>
viewCreationCache

Map from view key to View instance, synchronized for View creation.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
clearCache()

Clear the entire view cache, removing all cached view objects.

protected View

Returns:

the View instance, or null if not found (optional, to allow for ViewResolver chaining)
createView
(String
the name of the view to retrieve
viewName
,
Locale
the Locale to retrieve the view for
locale
)

Create the actual View object.

private static String
formatKey(Object cacheKey)

public AbstractCachingViewResolver.CacheFilter
getCacheFilter()

Return filter function that determines if view should be cached.

protected Object
getCacheKey(String viewName, Locale locale)

Return the cache key for the given view name and the given locale.

public int
getCacheLimit()

Return the maximum number of entries for the view cache.

public boolean
isCache()

Return if caching is enabled.

public boolean
isCacheUnresolved()

Return if caching of unresolved views is enabled.

protected abstract View

Returns:

the View instance, or null if not found (optional, to allow for ViewResolver chaining)
loadView
(String
the name of the view to retrieve
viewName
,
Locale
the Locale to retrieve the view for
locale
)

Subclasses must implement this method, building a View object for the specified view.

public void
removeFromCache(String
the view name for which the cached view object (if any) needs to be removed
viewName
,
Locale
the locale for which the view object should be removed
locale
)

Provides functionality to clear the cache for a certain view.

public View
resolveViewName(String
name of the view to resolve
viewName
,
Locale
the Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.
locale
)

Implements org.springframework.web.servlet.ViewResolver.resolveViewName.

Resolve the given view by name.

public void
setCache(boolean cache)

Enable or disable caching.

public void
setCacheFilter(AbstractCachingViewResolver.CacheFilter cacheFilter)

Set the filter that determines if view should be cached.

public void
setCacheLimit(int cacheLimit)

Specify the maximum number of entries for the view cache.

public void
setCacheUnresolved(boolean cacheUnresolved)

Whether a view name once resolved to null should be cached and automatically resolved to null subsequently.

Inherited from org.springframework.web.context.support.WebApplicationObjectSupport:
getServletContextgetTempDirgetWebApplicationContextinitApplicationContextinitServletContextisContextRequiredsetServletContext

Field Detail

cacheFilterback to summary
private AbstractCachingViewResolver.CacheFilter cacheFilter

Filter function that determines if view should be cached.

cacheLimitback to summary
private volatile int cacheLimit

The maximum number of entries in the cache.

cacheUnresolvedback to summary
private boolean cacheUnresolved

Whether we should refrain from resolving views again if unresolved once.

DEFAULT_CACHE_FILTERback to summary
private static final AbstractCachingViewResolver.CacheFilter DEFAULT_CACHE_FILTER

Default cache filter that always caches.

DEFAULT_CACHE_LIMITback to summary
public static final int DEFAULT_CACHE_LIMIT

Default maximum number of entries for the view cache: 1024.

UNRESOLVED_VIEWback to summary
private static final View UNRESOLVED_VIEW

Dummy marker object for unresolved views in the cache Maps.

viewAccessCacheback to summary
private final Map<Object, View> viewAccessCache

Fast access cache for Views, returning already cached instances without a global lock.

viewCreationCacheback to summary
private final Map<Object, View> viewCreationCache

Map from view key to View instance, synchronized for View creation.

Annotations
@SuppressWarnings:serial

Constructor Detail

AbstractCachingViewResolverback to summary
public AbstractCachingViewResolver()

Method Detail

clearCacheback to summary
public void clearCache()

Clear the entire view cache, removing all cached view objects. Subsequent resolve calls will lead to recreation of demanded view objects.

createViewback to summary
protected View createView(String viewName, Locale locale) throws Exception

Create the actual View object.

The default implementation delegates to loadView. This can be overridden to resolve certain view names in a special fashion, before delegating to the actual loadView implementation provided by the subclass.

Parameters
viewName:String

the name of the view to retrieve

locale:Locale

the Locale to retrieve the view for

Returns:View

the View instance, or null if not found (optional, to allow for ViewResolver chaining)

Annotations
@Nullable
Exceptions
Exception:
if the view couldn't be resolved
See Also
loadView
formatKeyback to summary
private static String formatKey(Object cacheKey)
getCacheFilterback to summary
public AbstractCachingViewResolver.CacheFilter getCacheFilter()

Return filter function that determines if view should be cached.

Since
5.2
getCacheKeyback to summary
protected Object getCacheKey(String viewName, Locale locale)

Return the cache key for the given view name and the given 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.

getCacheLimitback to summary
public int getCacheLimit()

Return the maximum number of entries for the view cache.

isCacheback to summary
public boolean isCache()

Return if caching is enabled.

isCacheUnresolvedback to summary
public boolean isCacheUnresolved()

Return if caching of unresolved views is enabled.

loadViewback to summary
protected abstract View loadView(String viewName, Locale locale) throws Exception

Subclasses 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.

Parameters
viewName:String

the name of the view to retrieve

locale:Locale

the Locale to retrieve the view for

Returns:View

the View instance, or null if not found (optional, to allow for ViewResolver chaining)

Annotations
@Nullable
Exceptions
Exception:
if the view couldn't be resolved
See Also
resolveViewName
removeFromCacheback to summary
public void removeFromCache(String viewName, Locale locale)

Provides functionality to clear the cache for a certain view.

This can be handy in case developers are able to modify views (e.g., FreeMarker templates) at runtime after which you'd need to clear the cache for the specified view.

Parameters
viewName:String

the view name for which the cached view object (if any) needs to be removed

locale:Locale

the locale for which the view object should be removed

resolveViewNameback to summary
public View resolveViewName(String viewName, Locale locale) throws Exception

Implements org.springframework.web.servlet.ViewResolver.resolveViewName.

Doc from org.springframework.web.servlet.ViewResolver.resolveViewName.

Resolve the given view by name.

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).

Parameters
viewName:String

name of the view to resolve

locale:Locale

the Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.

Returns:View

the View object, or null if not found (optional, to allow for ViewResolver chaining)

Annotations
@Override
@Nullable
Exceptions
Exception:
if the view cannot be resolved (typically in case of problems creating an actual View object)
setCacheback to summary
public void setCache(boolean cache)

Enable or disable caching.

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.

setCacheFilterback to summary
public void setCacheFilter(AbstractCachingViewResolver.CacheFilter cacheFilter)

Set the filter that determines if view should be cached.

Default behaviour is to cache all views.

Since
5.2
setCacheLimitback to summary
public void setCacheLimit(int cacheLimit)

Specify the maximum number of entries for the view cache. Default is 1024.

setCacheUnresolvedback to summary
public void setCacheUnresolved(boolean cacheUnresolved)

Whether a view name once resolved to null should be cached and automatically resolved to null subsequently.

Default is "true": unresolved view names are cached. 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 (e.g., FreeMarker) 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", only one check is made.

org.springframework.web.servlet.view back to summary

public Interface AbstractCachingViewResolver.CacheFilter

Annotations
@FunctionalInterface

Filter that determines if view should be cached.
Authors
Sergey Galkin, Arjen Poutsma
Since
5.2

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the view should be cached; false otherwise
filter
(View
the view
view
,
String
the name used to resolve the view
viewName
,
Locale
the locale used to resolve the view
locale
)

Indicates whether the given view should be cached.

Method Detail

filterback to summary
public boolean filter(View view, String viewName, Locale locale)

Indicates whether the given view should be cached. The name and locale used to resolve the view are also provided.

Parameters
view:View

the view

viewName:String

the name used to resolve the view

locale:Locale

the locale used to resolve the view

Returns:boolean

true if the view should be cached; false otherwise