1 /* 2 * Copyright 2002-2014 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.Locale; 20 21 import org.springframework.beans.BeansException; 22 import org.springframework.context.ApplicationContext; 23 import org.springframework.core.Ordered; 24 import org.springframework.web.context.support.WebApplicationObjectSupport; 25 import org.springframework.web.servlet.View; 26 import org.springframework.web.servlet.ViewResolver; 27 28 /** 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}. 32 * 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. 37 * 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. 41 * 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}. 47 * 48 * @author Juergen Hoeller 49 * @since 18.06.2003 50 * @see XmlViewResolver 51 * @see ResourceBundleViewResolver 52 * @see UrlBasedViewResolver 53 */ 54 public class BeanNameViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered { 55 56 private int order = Integer.MAX_VALUE; // default: same as non-Ordered 57 58 59 public void setOrder(int order) { 60 this.order = order; 61 } 62 63 @Override 64 public int getOrder() { 65 return this.order; 66 } 67 68 69 @Override 70 public View resolveViewName(String viewName, Locale locale) throws BeansException { 71 ApplicationContext context = getApplicationContext(); 72 if (!context.containsBean(viewName)) { 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... 86 return null; 87 } 88 return context.getBean(viewName, View.class); 89 } 90 91 }