Skip to content

Commit 035a9b9

Browse files
committed
BeanNameViewResolver ignores non-View beans
Issue: SPR-12079 (cherry picked from commit 626a5fe)
1 parent a7492fa commit 035a9b9

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/view/BeanNameViewResolver.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -26,25 +26,24 @@
2626
import org.springframework.web.servlet.ViewResolver;
2727

2828
/**
29-
* Simple implementation of ViewResolver that interprets a view name
30-
* as bean name in the current application context, i.e. in the XML
31-
* file of the executing DispatcherServlet.
29+
* A simple implementation of {@link org.springframework.web.servlet.ViewResolver}
30+
* that interprets a view name as a bean name in the current application context,
31+
* i.e. typically in the XML file of the executing {@code DispatcherServlet}.
3232
*
33-
* <p>This resolver can be handy for small applications, keeping all
34-
* definitions ranging from controllers to views in the same place.
35-
* For normal applications, XmlViewResolver will be the better choice, as
36-
* it separates the XML view bean definitions into a dedicated views file.
37-
* View beans should virtually never have references to any other
38-
* application beans - such a separation will make this clear.
33+
* <p>This resolver can be handy for small applications, keeping all definitions
34+
* ranging from controllers to views in the same place. For larger applications,
35+
* {@link XmlViewResolver} will be the better choice, as it separates the XML
36+
* view bean definitions into a dedicated views file.
3937
*
40-
* <p>This ViewResolver does not support internationalization.
41-
* Consider ResourceBundleViewResolver if you need to apply different
42-
* view resources per locale.
38+
* <p>Note: Neither this {@code ViewResolver} nor {@link XmlViewResolver} supports
39+
* internationalization. Consider {@link ResourceBundleViewResolver} if you need
40+
* to apply different view resources per locale.
4341
*
44-
* <p>Note: This ViewResolver implements the Ordered interface to allow for
45-
* flexible participation in ViewResolver chaining. For example, some special
46-
* views could be defined via this ViewResolver (giving it 0 as "order" value),
47-
* while all remaining views could be resolved by a UrlBasedViewResolver.
42+
* <p>Note: This {@code ViewResolver} implements the {@link Ordered} interface
43+
* in order to allow for flexible participation in {@code ViewResolver} chaining.
44+
* For example, some special views could be defined via this {@code ViewResolver}
45+
* (giving it 0 as "order" value), while all remaining views could be resolved by
46+
* a {@link UrlBasedViewResolver}.
4847
*
4948
* @author Juergen Hoeller
5049
* @since 18.06.2003
@@ -63,15 +62,27 @@ public void setOrder(int order) {
6362

6463
@Override
6564
public int getOrder() {
66-
return order;
65+
return this.order;
6766
}
6867

6968

7069
@Override
7170
public View resolveViewName(String viewName, Locale locale) throws BeansException {
7271
ApplicationContext context = getApplicationContext();
7372
if (!context.containsBean(viewName)) {
74-
// Allow for ViewResolver chaining.
73+
if (logger.isDebugEnabled()) {
74+
logger.debug("No matching bean found for view name '" + viewName + "'");
75+
}
76+
// Allow for ViewResolver chaining...
77+
return null;
78+
}
79+
if (!context.isTypeMatch(viewName, View.class)) {
80+
if (logger.isDebugEnabled()) {
81+
logger.debug("Found matching bean for view name '" + viewName +
82+
"' - to be ignored since it does not implement View");
83+
}
84+
// Since we're looking into the general ApplicationContext here,
85+
// let's accept this as a non-match and allow for chaining as well...
7586
return null;
7687
}
7788
return context.getBean(viewName, View.class);

0 commit comments

Comments
 (0)