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