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.velocity;
  17. import java.util.Locale;
  18. import org.springframework.beans.BeansException;
  19. import org.springframework.web.servlet.View;
  20. import org.springframework.web.servlet.view.UrlBasedViewResolver;
  21. /**
  22. * Convenience subclass of UrlBasedViewResolver that supports VelocityView
  23. * (i.e. Velocity templates) and custom subclasses of it.
  24. *
  25. * <p>The view class for all views generated by this resolver can be specified
  26. * via setViewClass. See UrlBasedViewResolver's javadocs for details.
  27. *
  28. * <p>Note: When chaining ViewResolvers, a VelocityViewResolver always needs
  29. * to be last, as it will attempt to resolve any view name, no matter whether
  30. * the underlying resource actually exists.
  31. *
  32. * @author Juergen Hoeller
  33. * @since 13.12.2003
  34. * @see #setViewClass
  35. * @see #setPrefix
  36. * @see #setSuffix
  37. * @see #setRequestContextAttribute
  38. * @see #setVelocityFormatterAttribute
  39. * @see #setDateToolAttribute
  40. * @see #setNumberToolAttribute
  41. * @see VelocityView
  42. */
  43. public class VelocityViewResolver extends UrlBasedViewResolver {
  44. private String velocityFormatterAttribute;
  45. private String dateToolAttribute;
  46. private String numberToolAttribute;
  47. /**
  48. * Sets default viewClass to VelocityView.
  49. * @see #setViewClass
  50. */
  51. public VelocityViewResolver() {
  52. setViewClass(VelocityView.class);
  53. }
  54. /**
  55. * Requires VelocityView.
  56. * @see VelocityView
  57. */
  58. protected Class requiredViewClass() {
  59. return VelocityView.class;
  60. }
  61. /**
  62. * Set the name of the VelocityFormatter helper object to expose in the
  63. * Velocity context of this view, or null if not needed.
  64. * VelocityFormatter is part of the standard Velocity distribution.
  65. * @see org.apache.velocity.app.tools.VelocityFormatter
  66. */
  67. public void setVelocityFormatterAttribute(String velocityFormatterAttribute) {
  68. this.velocityFormatterAttribute = velocityFormatterAttribute;
  69. }
  70. /**
  71. * Set the name of the DateTool helper object to expose in the Velocity context
  72. * of this view, or null if not needed. DateTool is part of Velocity Tools 1.0.
  73. * @see org.apache.velocity.tools.generic.DateTool
  74. */
  75. public void setDateToolAttribute(String dateToolAttribute) {
  76. this.dateToolAttribute = dateToolAttribute;
  77. }
  78. /**
  79. * Set the name of the NumberTool helper object to expose in the Velocity context
  80. * of this view, or null if not needed. NumberTool is part of Velocity Tools 1.1.
  81. * @see org.apache.velocity.tools.generic.NumberTool
  82. */
  83. public void setNumberToolAttribute(String numberToolAttribute) {
  84. this.numberToolAttribute = numberToolAttribute;
  85. }
  86. protected View loadView(String viewName, Locale locale) throws BeansException {
  87. VelocityView view = (VelocityView) super.loadView(viewName, locale);
  88. view.setVelocityFormatterAttribute(this.velocityFormatterAttribute);
  89. view.setDateToolAttribute(this.dateToolAttribute);
  90. view.setNumberToolAttribute(this.numberToolAttribute);
  91. return view;
  92. }
  93. }