01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.BeansException;
22: import org.springframework.context.ApplicationContext;
23: import org.springframework.core.Ordered;
24: import org.springframework.web.context.support.WebApplicationObjectSupport;
25: import org.springframework.web.servlet.View;
26: import org.springframework.web.servlet.ViewResolver;
27:
28: /**
29: * Simple implementation of ViewResolver that interprets a view name
30: * as bean name in the current application context, i.e. in the XML
31: * file of the executing DispatcherServlet.
32: *
33: * <p>This resolver can be handy for small applications, keeping all
34: * definitions ranging from controllers to views in the same place.
35: * For normal applications, XmlViewResolver will be the better choice, as
36: * it separates the XML view bean definitions into a dedicated views file.
37: * View beans should virtually never have references to any other
38: * application beans - such a separation will make this clear.
39: *
40: * <p>This ViewResolver does not support internationalization.
41: * Conside ResourceBundleViewResolver if you need to apply different
42: * view resources per locale.
43: *
44: * <p>Note: This ViewResolver implements the Ordered interface to allow for
45: * flexible participation in ViewResolver chaining. For example, some special
46: * views could be defined via this ViewResolver (giving it 0 as "order" value),
47: * while all remaining views could be resolved by a UrlBasedViewResolver.
48: *
49: * @author Juergen Hoeller
50: * @since 18.06.2003
51: * @see XmlViewResolver
52: * @see ResourceBundleViewResolver
53: * @see UrlBasedViewResolver
54: */
55: public class BeanNameViewResolver extends WebApplicationObjectSupport
56: implements ViewResolver, Ordered {
57:
58: private int order = Integer.MAX_VALUE; // default: same as non-Ordered
59:
60: public void setOrder(int order) {
61: this .order = order;
62: }
63:
64: public int getOrder() {
65: return order;
66: }
67:
68: public View resolveViewName(String viewName, Locale locale)
69: throws BeansException {
70: ApplicationContext context = getApplicationContext();
71: if (!context.containsBean(viewName)) {
72: // Allow for ViewResolver chaining.
73: return null;
74: }
75: return (View) context.getBean(viewName, View.class);
76: }
77:
78: }
|