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.config.annotation;
018
019import java.util.Map;
020
021import org.springframework.web.servlet.view.UrlBasedViewResolver;
022
023/**
024 * Assist with configuring a {@link org.springframework.web.servlet.view.UrlBasedViewResolver}.
025 *
026 * @author Sebastien Deleuze
027 * @author Rossen Stoyanchev
028 * @since 4.1
029 */
030public class UrlBasedViewResolverRegistration {
031
032        protected final UrlBasedViewResolver viewResolver;
033
034
035        public UrlBasedViewResolverRegistration(UrlBasedViewResolver viewResolver) {
036                this.viewResolver = viewResolver;
037        }
038
039
040        protected UrlBasedViewResolver getViewResolver() {
041                return this.viewResolver;
042        }
043
044        /**
045         * Set the prefix that gets prepended to view names when building a URL.
046         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setPrefix
047         */
048        public UrlBasedViewResolverRegistration prefix(String prefix) {
049                this.viewResolver.setPrefix(prefix);
050                return this;
051        }
052
053        /**
054         * Set the suffix that gets appended to view names when building a URL.
055         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setSuffix
056         */
057        public UrlBasedViewResolverRegistration suffix(String suffix) {
058                this.viewResolver.setSuffix(suffix);
059                return this;
060        }
061
062        /**
063         * Set the view class that should be used to create views.
064         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setViewClass
065         */
066        public UrlBasedViewResolverRegistration viewClass(Class<?> viewClass) {
067                this.viewResolver.setViewClass(viewClass);
068                return this;
069        }
070
071        /**
072         * Set the view names (or name patterns) that can be handled by this view
073         * resolver. View names can contain simple wildcards such that 'my*', '*Report'
074         * and '*Repo*' will all match the view name 'myReport'.
075         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setViewNames
076         */
077        public UrlBasedViewResolverRegistration viewNames(String... viewNames) {
078                this.viewResolver.setViewNames(viewNames);
079                return this;
080        }
081
082        /**
083         * Set static attributes to be added to the model of every request for all
084         * views resolved by this view resolver. This allows for setting any kind of
085         * attribute values, for example bean references.
086         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setAttributesMap
087         */
088        public UrlBasedViewResolverRegistration attributes(Map<String, ?> attributes) {
089                this.viewResolver.setAttributesMap(attributes);
090                return this;
091        }
092
093        /**
094         * Specify the maximum number of entries for the view cache.
095         * Default is 1024.
096         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setCache(boolean)
097         */
098        public UrlBasedViewResolverRegistration cacheLimit(int cacheLimit) {
099                this.viewResolver.setCacheLimit(cacheLimit);
100                return this;
101        }
102
103        /**
104         * Enable or disable caching.
105         * <p>This is equivalent to setting the {@link #cacheLimit "cacheLimit"}
106         * property to the default limit (1024) or to 0, respectively.
107         * <p>Default is "true": caching is enabled.
108         * Disable this only for debugging and development.
109         * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setCache(boolean)
110         */
111        public UrlBasedViewResolverRegistration cache(boolean cache) {
112                this.viewResolver.setCache(cache);
113                return this;
114        }
115
116}
117
118