KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > velocity > VelocityViewResolver


1 /*
2  * Copyright 2002-2006 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.velocity;
18
19 import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
20 import org.springframework.web.servlet.view.AbstractUrlBasedView;
21
22 /**
23  * Convenience subclass of UrlBasedViewResolver that supports VelocityView
24  * (that is Velocity templates) and custom subclasses of it.
25  *
26  * <p>The view class for all views generated by this resolver can be specified
27  * via <code>setViewClass</code>. See UrlBasedViewResolver's javadoc for details.
28  *
29  * <p><b>Note:</b> When chaining ViewResolvers, a VelocityViewResolver always needs
30  * to be last, as it will attempt to resolve any view name, no matter whether
31  * the underlying resource actually exists.
32  *
33  * @author Juergen Hoeller
34  * @since 13.12.2003
35  * @see #setViewClass
36  * @see #setPrefix
37  * @see #setSuffix
38  * @see #setRequestContextAttribute
39  * @see #setExposeSpringMacroHelpers
40  * @see #setVelocityFormatterAttribute
41  * @see #setDateToolAttribute
42  * @see #setNumberToolAttribute
43  * @see VelocityView
44  */

45 public class VelocityViewResolver extends AbstractTemplateViewResolver {
46
47     private String JavaDoc velocityFormatterAttribute;
48
49     private String JavaDoc dateToolAttribute;
50
51     private String JavaDoc numberToolAttribute;
52
53     private String JavaDoc toolboxConfigLocation;
54
55
56     /**
57      * Sets default viewClass to <code>requiredViewClass</code>.
58      * @see #setViewClass
59      * @see #requiredViewClass
60      */

61     public VelocityViewResolver() {
62         setViewClass(requiredViewClass());
63     }
64
65     /**
66      * Requires VelocityView.
67      * @see VelocityView
68      */

69     protected Class JavaDoc requiredViewClass() {
70         return VelocityView.class;
71     }
72
73     /**
74      * Set the name of the VelocityFormatter helper object to expose in the
75      * Velocity context of this view, or <code>null</code> if not needed.
76      * VelocityFormatter is part of the standard Velocity distribution.
77      * @see org.apache.velocity.app.tools.VelocityFormatter
78      * @see VelocityView#setVelocityFormatterAttribute
79      */

80     public void setVelocityFormatterAttribute(String JavaDoc velocityFormatterAttribute) {
81         this.velocityFormatterAttribute = velocityFormatterAttribute;
82     }
83
84     /**
85      * Set the name of the DateTool helper object to expose in the Velocity context
86      * of this view, or <code>null</code> if not needed. DateTool is part of Velocity Tools 1.0.
87      * @see org.apache.velocity.tools.generic.DateTool
88      * @see VelocityView#setDateToolAttribute
89      */

90     public void setDateToolAttribute(String JavaDoc dateToolAttribute) {
91         this.dateToolAttribute = dateToolAttribute;
92     }
93
94     /**
95      * Set the name of the NumberTool helper object to expose in the Velocity context
96      * of this view, or <code>null</code> if not needed. NumberTool is part of Velocity Tools 1.1.
97      * @see org.apache.velocity.tools.generic.NumberTool
98      * @see VelocityView#setNumberToolAttribute
99      */

100     public void setNumberToolAttribute(String JavaDoc 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 JavaDoc toolboxConfigLocation) {
118         this.toolboxConfigLocation = toolboxConfigLocation;
119     }
120
121
122     protected void initApplicationContext() {
123         super.initApplicationContext();
124
125         if (this.toolboxConfigLocation != null) {
126             if (VelocityView.class.equals(getViewClass())) {
127                 logger.info("Using VelocityToolboxView instead of default VelocityView " +
128                         "due to specified toolboxConfigLocation");
129                 setViewClass(VelocityToolboxView.class);
130             }
131             else if (!VelocityToolboxView.class.isAssignableFrom(getViewClass())) {
132                 throw new IllegalArgumentException JavaDoc(
133                         "Given view class [" + getViewClass().getName() +
134                         "] is not of type [" + VelocityToolboxView.class.getName() +
135                         "], which it needs to be in case of a specified toolboxConfigLocation");
136             }
137         }
138     }
139
140
141     protected AbstractUrlBasedView buildView(String JavaDoc viewName) throws Exception JavaDoc {
142         VelocityView view = (VelocityView) super.buildView(viewName);
143         view.setVelocityFormatterAttribute(this.velocityFormatterAttribute);
144         view.setDateToolAttribute(this.dateToolAttribute);
145         view.setNumberToolAttribute(this.numberToolAttribute);
146         if (this.toolboxConfigLocation != null) {
147             ((VelocityToolboxView) view).setToolboxConfigLocation(this.toolboxConfigLocation);
148         }
149         return view;
150     }
151
152 }
153
Popular Tags