1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.servlet.view;
  17. import java.util.Locale;
  18. import org.springframework.beans.BeansException;
  19. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  20. import org.springframework.core.Ordered;
  21. import org.springframework.web.context.support.WebApplicationObjectSupport;
  22. import org.springframework.web.servlet.View;
  23. import org.springframework.web.servlet.ViewResolver;
  24. /**
  25. * Simple implementation of ViewResolver that interprets a view name
  26. * as bean name in the current application context, i.e. in the XML
  27. * file of the executing DispatcherServlet.
  28. *
  29. * <p>This resolver can be handy for small applications, keeping all
  30. * definitions ranging from controllers to views in the same place.
  31. * For normal applications, XmlViewResolver will be the better choice, as
  32. * it separates the XML view bean definitions into a dedicated views file.
  33. * View beans should virtually never have references to any other
  34. * application beans - such a separation will make this clear.
  35. *
  36. * <p>This ViewResolver does not support internationalization.
  37. * Conside ResourceBundleViewResolver if you need to apply different
  38. * view resources per locale.
  39. *
  40. * <p>Note: This ViewResolver implements the Ordered interface to allow for
  41. * flexible participation in ViewResolver chaining. For example, some special
  42. * views could be defined via this ViewResolver (giving it 0 as "order" value),
  43. * while all remaining views could be resolved by a UrlBasedViewResolver.
  44. *
  45. * @author Juergen Hoeller
  46. * @since 18.06.2003
  47. * @see XmlViewResolver
  48. * @see ResourceBundleViewResolver
  49. * @see UrlBasedViewResolver
  50. */
  51. public class BeanNameViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered {
  52. private int order = Integer.MAX_VALUE; // default: same as non-Ordered
  53. public void setOrder(int order) {
  54. this.order = order;
  55. }
  56. public int getOrder() {
  57. return order;
  58. }
  59. public View resolveViewName(String viewName, Locale locale) throws BeansException {
  60. try {
  61. return (View) getApplicationContext().getBean(viewName, View.class);
  62. }
  63. catch (NoSuchBeanDefinitionException ex) {
  64. // to allow for ViewResolver chaining
  65. return null;
  66. }
  67. }
  68. }