001/* 002 * Copyright 2002-2014 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.ArrayList; 020import java.util.Collections; 021import java.util.List; 022import java.util.Locale; 023import javax.servlet.ServletContext; 024 025import org.springframework.beans.BeansException; 026import org.springframework.beans.factory.InitializingBean; 027import org.springframework.context.ApplicationContext; 028import org.springframework.context.ApplicationContextAware; 029import org.springframework.core.Ordered; 030import org.springframework.util.CollectionUtils; 031import org.springframework.web.context.ServletContextAware; 032import org.springframework.web.servlet.View; 033import org.springframework.web.servlet.ViewResolver; 034 035/** 036 * A {@link org.springframework.web.servlet.ViewResolver} that delegates to others. 037 * 038 * @author Sebastien Deleuze 039 * @author Rossen Stoyanchev 040 * @since 4.1 041 */ 042public class ViewResolverComposite implements ViewResolver, Ordered, InitializingBean, 043 ApplicationContextAware, ServletContextAware { 044 045 private final List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(); 046 047 private int order = Ordered.LOWEST_PRECEDENCE; 048 049 050 /** 051 * Set the list of view viewResolvers to delegate to. 052 */ 053 public void setViewResolvers(List<ViewResolver> viewResolvers) { 054 this.viewResolvers.clear(); 055 if (!CollectionUtils.isEmpty(viewResolvers)) { 056 this.viewResolvers.addAll(viewResolvers); 057 } 058 } 059 060 /** 061 * Return the list of view viewResolvers to delegate to. 062 */ 063 public List<ViewResolver> getViewResolvers() { 064 return Collections.unmodifiableList(this.viewResolvers); 065 } 066 067 public void setOrder(int order) { 068 this.order = order; 069 } 070 071 @Override 072 public int getOrder() { 073 return this.order; 074 } 075 076 @Override 077 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 078 for (ViewResolver viewResolver : this.viewResolvers) { 079 if (viewResolver instanceof ApplicationContextAware) { 080 ((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext); 081 } 082 } 083 } 084 085 @Override 086 public void setServletContext(ServletContext servletContext) { 087 for (ViewResolver viewResolver : this.viewResolvers) { 088 if (viewResolver instanceof ServletContextAware) { 089 ((ServletContextAware)viewResolver).setServletContext(servletContext); 090 } 091 } 092 } 093 094 @Override 095 public void afterPropertiesSet() throws Exception { 096 for (ViewResolver viewResolver : this.viewResolvers) { 097 if (viewResolver instanceof InitializingBean) { 098 ((InitializingBean) viewResolver).afterPropertiesSet(); 099 } 100 } 101 } 102 103 @Override 104 public View resolveViewName(String viewName, Locale locale) throws Exception { 105 for (ViewResolver viewResolver : this.viewResolvers) { 106 View view = viewResolver.resolveViewName(viewName, locale); 107 if (view != null) { 108 return view; 109 } 110 } 111 return null; 112 } 113 114}