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