001/*
002 * Copyright 2002-2017 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.factory.InitializingBean;
022import org.springframework.lang.Nullable;
023
024/**
025 * Abstract base class for URL-based views. Provides a consistent way of
026 * holding the URL that a View wraps, in the form of a "url" bean property.
027 *
028 * @author Juergen Hoeller
029 * @since 13.12.2003
030 */
031public abstract class AbstractUrlBasedView extends AbstractView implements InitializingBean {
032
033        @Nullable
034        private String url;
035
036
037        /**
038         * Constructor for use as a bean.
039         */
040        protected AbstractUrlBasedView() {
041        }
042
043        /**
044         * Create a new AbstractUrlBasedView with the given URL.
045         * @param url the URL to forward to
046         */
047        protected AbstractUrlBasedView(String url) {
048                this.url = url;
049        }
050
051
052        /**
053         * Set the URL of the resource that this view wraps.
054         * The URL must be appropriate for the concrete View implementation.
055         */
056        public void setUrl(@Nullable String url) {
057                this.url = url;
058        }
059
060        /**
061         * Return the URL of the resource that this view wraps.
062         */
063        @Nullable
064        public String getUrl() {
065                return this.url;
066        }
067
068        @Override
069        public void afterPropertiesSet() throws Exception {
070                if (isUrlRequired() && getUrl() == null) {
071                        throw new IllegalArgumentException("Property 'url' is required");
072                }
073        }
074
075        /**
076         * Return whether the 'url' property is required.
077         * <p>The default implementation returns {@code true}.
078         * This can be overridden in subclasses.
079         */
080        protected boolean isUrlRequired() {
081                return true;
082        }
083
084        /**
085         * Check whether the underlying resource that the configured URL points to
086         * actually exists.
087         * @param locale the desired Locale that we're looking for
088         * @return {@code true} if the resource exists (or is assumed to exist);
089         * {@code false} if we know that it does not exist
090         * @throws Exception if the resource exists but is invalid (e.g. could not be parsed)
091         */
092        public boolean checkResource(Locale locale) throws Exception {
093                return true;
094        }
095
096        @Override
097        public String toString() {
098                return super.toString() + "; URL [" + getUrl() + "]";
099        }
100
101}