forked from spring-projects/spring-boot
/
VelocityToolboxView.java
134 lines (118 loc) · 5.29 KB
/
VelocityToolboxView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.velocity;
import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* {@link VelocityView} subclass which adds support for Velocity Tools toolboxes
* and Velocity Tools ViewTool callbacks / Velocity Tools 1.3 init methods.
*
* <p>Specify a "toolboxConfigLocation", for example "/WEB-INF/toolbox.xml",
* to automatically load a Velocity Tools toolbox definition file and expose
* all defined tools in the specified scopes. If no config location is
* specified, no toolbox will be loaded and exposed.
*
* <p>This view will always create a special Velocity context, namely an
* instance of the ChainedContext class which is part of the view package
* of Velocity tools. This allows to use tools from the view package of
* Velocity Tools, like LinkTool, which need to be initialized with a special
* context that implements the ViewContext interface (i.e. a ChainedContext).
*
* <p>This view also checks tools that are specified as "toolAttributes":
* If they implement the ViewTool interface, they will get initialized with
* the Velocity context. This allows tools from the view package of Velocity
* Tools, such as LinkTool, to be defined as
* {@link #setToolAttributes "toolAttributes"} on a VelocityToolboxView,
* instead of in a separate toolbox XML file.
*
* <p>This is a separate class mainly to avoid a required dependency on
* the view package of Velocity Tools in {@link VelocityView} itself.
* As of Spring 3.0, this class requires Velocity Tools 1.3 or higher.
*
* @author Juergen Hoeller
* @since 1.1.3
* @see #setToolboxConfigLocation
* @see #initTool
* @see org.apache.velocity.tools.view.context.ViewContext
* @see org.apache.velocity.tools.view.context.ChainedContext
* @deprecated as of Spring 4.3, in favor of FreeMarker
*/
@Deprecated
public class VelocityToolboxView extends VelocityView {
private String toolboxConfigLocation;
/**
* Set a Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml",
* to automatically load a Velocity Tools toolbox definition file and expose
* all defined tools in the specified scopes. If no config location is
* specified, no toolbox will be loaded and exposed.
* <p>The specified location string needs to refer to a ServletContext
* resource, as expected by ServletToolboxManager which is part of
* the view package of Velocity Tools.
* @see org.apache.velocity.tools.view.servlet.ServletToolboxManager#getInstance
*/
public void setToolboxConfigLocation(String toolboxConfigLocation) {
this.toolboxConfigLocation = toolboxConfigLocation;
}
/**
* Return the Velocity Toolbox config location, if any.
*/
protected String getToolboxConfigLocation() {
return this.toolboxConfigLocation;
}
/**
* Overridden to create a ChainedContext, which is part of the view package
* of Velocity Tools, as special context. ChainedContext is needed for
* initialization of ViewTool instances.
* @see #initTool
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Context createVelocityContext(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Create a ChainedContext instance.
org.apache.velocity.tools.view.context.ChainedContext velocityContext =
new org.apache.velocity.tools.view.context.ChainedContext(
new VelocityContext(model), getVelocityEngine(), request, response, getServletContext());
// Load a Velocity Tools toolbox, if necessary.
if (getToolboxConfigLocation() != null) {
org.apache.velocity.tools.view.ToolboxManager toolboxManager =
org.apache.velocity.tools.view.servlet.ServletToolboxManager.getInstance(
getServletContext(), getToolboxConfigLocation());
Map toolboxContext = toolboxManager.getToolbox(velocityContext);
velocityContext.setToolbox(toolboxContext);
}
return velocityContext;
}
/**
* Overridden to check for the ViewContext interface which is part of the
* view package of Velocity Tools. This requires a special Velocity context,
* like ChainedContext as set up by {@link #createVelocityContext} in this class.
*/
@Override
protected void initTool(Object tool, Context velocityContext) throws Exception {
// Velocity Tools 1.3: a class-level "init(Object)" method.
Method initMethod = ClassUtils.getMethodIfAvailable(tool.getClass(), "init", Object.class);
if (initMethod != null) {
ReflectionUtils.invokeMethod(initMethod, tool, velocityContext);
}
}
}