001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.view; 018 019import java.util.Locale; 020 021import org.springframework.beans.BeansException; 022import org.springframework.context.ApplicationContext; 023import org.springframework.core.Ordered; 024import org.springframework.web.context.support.WebApplicationObjectSupport; 025import org.springframework.web.servlet.View; 026import org.springframework.web.servlet.ViewResolver; 027 028/** 029 * A simple implementation of {@link org.springframework.web.servlet.ViewResolver} 030 * that interprets a view name as a bean name in the current application context, 031 * i.e. typically in the XML file of the executing {@code DispatcherServlet}. 032 * 033 * <p>This resolver can be handy for small applications, keeping all definitions 034 * ranging from controllers to views in the same place. For larger applications, 035 * {@link XmlViewResolver} will be the better choice, as it separates the XML 036 * view bean definitions into a dedicated views file. 037 * 038 * <p>Note: Neither this {@code ViewResolver} nor {@link XmlViewResolver} supports 039 * internationalization. Consider {@link ResourceBundleViewResolver} if you need 040 * to apply different view resources per locale. 041 * 042 * <p>Note: This {@code ViewResolver} implements the {@link Ordered} interface 043 * in order to allow for flexible participation in {@code ViewResolver} chaining. 044 * For example, some special views could be defined via this {@code ViewResolver} 045 * (giving it 0 as "order" value), while all remaining views could be resolved by 046 * a {@link UrlBasedViewResolver}. 047 * 048 * @author Juergen Hoeller 049 * @since 18.06.2003 050 * @see XmlViewResolver 051 * @see ResourceBundleViewResolver 052 * @see UrlBasedViewResolver 053 */ 054public class BeanNameViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered { 055 056 private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered 057 058 059 /** 060 * Specify the order value for this ViewResolver bean. 061 * <p>The default value is {@code Ordered.LOWEST_PRECEDENCE}, meaning non-ordered. 062 * @see org.springframework.core.Ordered#getOrder() 063 */ 064 public void setOrder(int order) { 065 this.order = order; 066 } 067 068 @Override 069 public int getOrder() { 070 return this.order; 071 } 072 073 074 @Override 075 public View resolveViewName(String viewName, Locale locale) throws BeansException { 076 ApplicationContext context = getApplicationContext(); 077 if (!context.containsBean(viewName)) { 078 if (logger.isDebugEnabled()) { 079 logger.debug("No matching bean found for view name '" + viewName + "'"); 080 } 081 // Allow for ViewResolver chaining... 082 return null; 083 } 084 if (!context.isTypeMatch(viewName, View.class)) { 085 if (logger.isDebugEnabled()) { 086 logger.debug("Found matching bean for view name '" + viewName + 087 "' - to be ignored since it does not implement View"); 088 } 089 // Since we're looking into the general ApplicationContext here, 090 // let's accept this as a non-match and allow for chaining as well... 091 return null; 092 } 093 return context.getBean(viewName, View.class); 094 } 095 096}