001: /*
002: * Copyright 2002-2006 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: * http://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:
017: package org.springframework.web.servlet.view.velocity;
018:
019: import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
020: import org.springframework.web.servlet.view.AbstractUrlBasedView;
021:
022: /**
023: * Convenience subclass of UrlBasedViewResolver that supports VelocityView
024: * (that is Velocity templates) and custom subclasses of it.
025: *
026: * <p>The view class for all views generated by this resolver can be specified
027: * via <code>setViewClass</code>. See UrlBasedViewResolver's javadoc for details.
028: *
029: * <p><b>Note:</b> When chaining ViewResolvers, a VelocityViewResolver always needs
030: * to be last, as it will attempt to resolve any view name, no matter whether
031: * the underlying resource actually exists.
032: *
033: * @author Juergen Hoeller
034: * @since 13.12.2003
035: * @see #setViewClass
036: * @see #setPrefix
037: * @see #setSuffix
038: * @see #setRequestContextAttribute
039: * @see #setExposeSpringMacroHelpers
040: * @see #setVelocityFormatterAttribute
041: * @see #setDateToolAttribute
042: * @see #setNumberToolAttribute
043: * @see VelocityView
044: */
045: public class VelocityViewResolver extends AbstractTemplateViewResolver {
046:
047: private String velocityFormatterAttribute;
048:
049: private String dateToolAttribute;
050:
051: private String numberToolAttribute;
052:
053: private String toolboxConfigLocation;
054:
055: /**
056: * Sets default viewClass to <code>requiredViewClass</code>.
057: * @see #setViewClass
058: * @see #requiredViewClass
059: */
060: public VelocityViewResolver() {
061: setViewClass(requiredViewClass());
062: }
063:
064: /**
065: * Requires VelocityView.
066: * @see VelocityView
067: */
068: protected Class requiredViewClass() {
069: return VelocityView.class;
070: }
071:
072: /**
073: * Set the name of the VelocityFormatter helper object to expose in the
074: * Velocity context of this view, or <code>null</code> if not needed.
075: * VelocityFormatter is part of the standard Velocity distribution.
076: * @see org.apache.velocity.app.tools.VelocityFormatter
077: * @see VelocityView#setVelocityFormatterAttribute
078: */
079: public void setVelocityFormatterAttribute(
080: String velocityFormatterAttribute) {
081: this .velocityFormatterAttribute = velocityFormatterAttribute;
082: }
083:
084: /**
085: * Set the name of the DateTool helper object to expose in the Velocity context
086: * of this view, or <code>null</code> if not needed. DateTool is part of Velocity Tools 1.0.
087: * @see org.apache.velocity.tools.generic.DateTool
088: * @see VelocityView#setDateToolAttribute
089: */
090: public void setDateToolAttribute(String dateToolAttribute) {
091: this .dateToolAttribute = dateToolAttribute;
092: }
093:
094: /**
095: * Set the name of the NumberTool helper object to expose in the Velocity context
096: * of this view, or <code>null</code> if not needed. NumberTool is part of Velocity Tools 1.1.
097: * @see org.apache.velocity.tools.generic.NumberTool
098: * @see VelocityView#setNumberToolAttribute
099: */
100: public void setNumberToolAttribute(String numberToolAttribute) {
101: this .numberToolAttribute = numberToolAttribute;
102: }
103:
104: /**
105: * Set a Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml",
106: * to automatically load a Velocity Tools toolbox definition file and expose
107: * all defined tools in the specified scopes. If no config location is
108: * specified, no toolbox will be loaded and exposed.
109: * <p>The specfied location string needs to refer to a ServletContext
110: * resource, as expected by ServletToolboxManager which is part of
111: * the view package of Velocity Tools.
112: * <p><b>Note:</b> Specifying a toolbox config location will lead to
113: * VelocityToolboxView instances being created.
114: * @see org.apache.velocity.tools.view.servlet.ServletToolboxManager#getInstance
115: * @see VelocityToolboxView#setToolboxConfigLocation
116: */
117: public void setToolboxConfigLocation(String toolboxConfigLocation) {
118: this .toolboxConfigLocation = toolboxConfigLocation;
119: }
120:
121: protected void initApplicationContext() {
122: super .initApplicationContext();
123:
124: if (this .toolboxConfigLocation != null) {
125: if (VelocityView.class.equals(getViewClass())) {
126: logger
127: .info("Using VelocityToolboxView instead of default VelocityView "
128: + "due to specified toolboxConfigLocation");
129: setViewClass(VelocityToolboxView.class);
130: } else if (!VelocityToolboxView.class
131: .isAssignableFrom(getViewClass())) {
132: throw new IllegalArgumentException(
133: "Given view class ["
134: + getViewClass().getName()
135: + "] is not of type ["
136: + VelocityToolboxView.class.getName()
137: + "], which it needs to be in case of a specified toolboxConfigLocation");
138: }
139: }
140: }
141:
142: protected AbstractUrlBasedView buildView(String viewName)
143: throws Exception {
144: VelocityView view = (VelocityView) super .buildView(viewName);
145: view
146: .setVelocityFormatterAttribute(this .velocityFormatterAttribute);
147: view.setDateToolAttribute(this .dateToolAttribute);
148: view.setNumberToolAttribute(this .numberToolAttribute);
149: if (this .toolboxConfigLocation != null) {
150: ((VelocityToolboxView) view)
151: .setToolboxConfigLocation(this.toolboxConfigLocation);
152: }
153: return view;
154: }
155:
156: }
|