Velocity is a template Java template language. For more information on Velocity itself, please visit the Velocity website.
Getting StartedGetting started with Velocity is as simple as ensuring all the dependencies are included in your project's classpath. Other than that, webwork-default.xml already configures the Velocity Result needed to map your actions to your templates. You may now try out the following xwork.xml configuration: <action name="test" class="com.acme.TestAction"> <result name="success" type="velocity">test-success.vm</result> </action> Then in test-success.vm: <html> <head> <title>Hello</title> </head> <body> Hello, ${name} </body> </html> Where name is a property on your action. That's it! Read the rest of this document for details on how templates are loaded, variables are resolved, and tags can be used. Template LoadingWebWork looks for Velocity templates in two locations (in this order):
This ordering makes it ideal for providing templates inside a fully built jar, but allowing for overrides of those templates to be defined in your web application. In fact, this is how you can override the default UI tags and Form Tags included with WebWork. Variable ResolutionIn Velocity, variables are looked up in several different places, in this order:
Note that the action context is looked up after the value stack. This means that you can reference the variable without the typical preceding has marker (#) like you would have to when using the JSP ww:property tag. This is a nice convenience, though be careful because there is a small chance it could trip you up. #wwurl "id=url "value=http://www.yahoo.com" Click <a href="${url}">here</a>! The built-in variables that WebWork-Velocity integration provides are:
Tag SupportSee the Velocity Tags documentation for information on how to use the generic Tags provided by WebWork. Tips and TricksThere are some advanced features that may be useful when building WebWork applications with Velocity. ExtendingSometimes you may with to extend the Velocity support provided with WebWork. The most common reason for doing this is that you wish to include your own Tags, such as those that you have extended from the built in WebWork Tags. To do so, write a new class that extends com.opensymphony.webwork.views.velocity.VelocityManager and overrides it as needed. Then add the following to webwork.properties: webwork.velocity.manager.classname = com.yourcompany.YourVelocityManager ConfiguringYou can configure Velocity by placing configuration items in velocity.properties. |