View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.view;
18  
19  import java.util.Locale;
20  
21  import org.springframework.beans.factory.InitializingBean;
22  
23  /**
24   * Abstract base class for URL-based views. Provides a consistent way of
25   * holding the URL that a View wraps, in the form of a "url" bean property.
26   *
27   * @author Juergen Hoeller
28   * @since 13.12.2003
29   */
30  public abstract class AbstractUrlBasedView extends AbstractView implements InitializingBean {
31  
32  	private String url;
33  
34  
35  	/**
36  	 * Constructor for use as a bean.
37  	 */
38  	protected AbstractUrlBasedView() {
39  	}
40  
41  	/**
42  	 * Create a new AbstractUrlBasedView with the given URL.
43  	 * @param url the URL to forward to
44  	 */
45  	protected AbstractUrlBasedView(String url) {
46  		this.url = url;
47  	}
48  
49  
50  	/**
51  	 * Set the URL of the resource that this view wraps.
52  	 * The URL must be appropriate for the concrete View implementation.
53  	 */
54  	public void setUrl(String url) {
55  		this.url = url;
56  	}
57  
58  	/**
59  	 * Return the URL of the resource that this view wraps.
60  	 */
61  	public String getUrl() {
62  		return this.url;
63  	}
64  
65  	@Override
66  	public void afterPropertiesSet() throws Exception {
67  		if (isUrlRequired() && getUrl() == null) {
68  			throw new IllegalArgumentException("Property 'url' is required");
69  		}
70  	}
71  
72  	/**
73  	 * Return whether the 'url' property is required.
74  	 * <p>The default implementation returns {@code true}.
75  	 * This can be overridden in subclasses.
76  	 */
77  	protected boolean isUrlRequired() {
78  		return true;
79  	}
80  
81  	/**
82  	 * Check whether the underlying resource that the configured URL points to
83  	 * actually exists.
84  	 * @param locale the desired Locale that we're looking for
85  	 * @return {@code true} if the resource exists (or is assumed to exist);
86  	 * {@code false} if we know that it does not exist
87  	 * @throws Exception if the resource exists but is invalid (e.g. could not be parsed)
88  	 */
89  	public boolean checkResource(Locale locale) throws Exception {
90  		return true;
91  	}
92  
93  	@Override
94  	public String toString() {
95  		StringBuilder sb = new StringBuilder(super.toString());
96  		sb.append("; URL [").append(getUrl()).append("]");
97  		return sb.toString();
98  	}
99  
100 }