Skip to content

Commit 1bb6d29

Browse files
committed
AbstractCachingViewResolver caches unresolved view names by default ("cacheUnresolved"=true; SPR-8173)
1 parent 6991cd9 commit 1bb6d29

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/AbstractCachingViewResolver.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,8 +42,8 @@ public abstract class AbstractCachingViewResolver extends WebApplicationObjectSu
4242
/** Whether we should cache views, once resolved */
4343
private boolean cache = true;
4444

45-
/** Whether we should attempt to resolve views again if unresolved once */
46-
private boolean cacheUnresolved = false;
45+
/** Whether we should refrain from resolving views again if unresolved once */
46+
private boolean cacheUnresolved = true;
4747

4848
/** Map from view key to View instance */
4949
private final Map<Object, View> viewCache = new HashMap<Object, View>();
@@ -65,15 +65,17 @@ public void setCache(boolean cache) {
6565
public boolean isCache() {
6666
return this.cache;
6767
}
68-
68+
6969
/**
70-
* Whether a view name once resolved to {@code null} should be cached and
70+
* Whether a view name once resolved to {@code null} should be cached and
7171
* automatically resolved to {@code null} subsequently.
72-
* <p>Default is "false": unresolved view names are not cached.
73-
* <p>Of specific interest is the ability for some AbstractUrlBasedView
74-
* implementations (Freemarker, Velocity, Tiles) to check if an underlying
72+
* <p>Default is "true": unresolved view names are being cached, as of Spring 3.1.
73+
* Note that this flag only applies if the general {@link #setCache "cache"}
74+
* flag is kept at its default of "true" as well.
75+
* <p>Of specific interest is the ability for some AbstractUrlBasedView
76+
* implementations (FreeMarker, Velocity, Tiles) to check if an underlying
7577
* resource exists via {@link AbstractUrlBasedView#checkResource(Locale)}.
76-
* With this flag set to "false", an underlying resource that re-appears
78+
* With this flag set to "false", an underlying resource that re-appears
7779
* is noticed and used. With the flag set to "true", one check is made only.
7880
*/
7981
public void setCacheUnresolved(boolean cacheUnresolved) {
@@ -84,9 +86,10 @@ public void setCacheUnresolved(boolean cacheUnresolved) {
8486
* Return if caching of unresolved views is enabled.
8587
*/
8688
public boolean isCacheUnresolved() {
87-
return cacheUnresolved;
89+
return this.cacheUnresolved;
8890
}
8991

92+
9093
public View resolveViewName(String viewName, Locale locale) throws Exception {
9194
if (!isCache()) {
9295
return createView(viewName, locale);
@@ -95,13 +98,14 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
9598
Object cacheKey = getCacheKey(viewName, locale);
9699
synchronized (this.viewCache) {
97100
View view = this.viewCache.get(cacheKey);
98-
boolean isCached = this.cacheUnresolved && this.viewCache.containsKey(cacheKey);
99-
if (view == null && !isCached) {
101+
if (view == null && (!this.cacheUnresolved || !this.viewCache.containsKey(cacheKey))) {
100102
// Ask the subclass to create the View object.
101103
view = createView(viewName, locale);
102-
this.viewCache.put(cacheKey, view);
103-
if (logger.isTraceEnabled()) {
104-
logger.trace("Cached view [" + cacheKey + "]");
104+
if (view != null || this.cacheUnresolved) {
105+
this.viewCache.put(cacheKey, view);
106+
if (logger.isTraceEnabled()) {
107+
logger.trace("Cached view [" + cacheKey + "]");
108+
}
105109
}
106110
}
107111
return view;

0 commit comments

Comments
 (0)