Home > Guides > Tag Developers Guide > Velocity |
Velocity is a templating language for Java.
For more information on Velocity itself, please visit the Velocity website.
Getting started with Velocity is as simple as ensuring all the dependencies are included in your project's classpath. Other than that, struts-default.xml already configures the Velocity Result.
Where name
is a property on the Action class. That's it!
There are few more details of interest, such as how templates are loaded and variables are resolved.
The framework looks for Velocity templates in two locations (in this order):
The ordering is designed so that a default set of templates can be placed in a JAR (perhaps shared between applications). If a template needs to be overridden, a different version can be placed in the web application.
In Velocity, there are three sources for variables, searched in a specific order.
Since the action context is resolved after the value stack, you can reference the variable without the typical preceding marker (#) that has to be used with the JSP s:property
tag. Omitting the marker can be convenient, but it can also trip you up, if used carelessly.
The Stuts2-Velocity integration layer provides several implicit variables.
Variable | Description |
---|---|
stack | The value stack itself, useful for calls like ${stack.findString('ognl expr')} |
action | The action most recently executed |
response | The HttpServletResponse |
res | Same as response |
request | The HttpServletRequest |
req | Same as request |
session | The HttpSession |
application | The ServletContext |
base | The request's context path |
You can configure Velocity by placing configuration items in velocity.properties.