Professional Documents
Culture Documents
Velocity 2pp
Velocity 2pp
Velocity
Bob Tarr
What Is Velocity?
l
l
l
l
l
Introduction To Velocity
2
Bob Tarr
References to objects
Static content
Directives that execute control structures, such as loops
Introduction To Velocity
3
Bob Tarr
Templates
l
l
l
l
Introduction To Velocity
4
Bob Tarr
WebMacro
FreeMarker
Introduction To Velocity
5
Bob Tarr
Basic objects
l
l
Introduction To Velocity
6
Bob Tarr
Variable
Property
Method
Directives
#foreach()
#if(), #elseif(), #else
#set()
#macro()
#include()
#parse()
#end
#stop
Introduction To Velocity
7
Bob Tarr
Using Velocity
l
l
l
l
l
Introduction To Velocity
8
Bob Tarr
Similar to a Hashtable
Has methods to add and retrieve items
Basic methods:
Object put(String key, Object value)
Adds a name/value pair to the context.
Object get(String key)
Gets the value corresponding to the provided key
from the context.
Introduction To Velocity
9
Bob Tarr
java.io.StringWriter;
org.apache.velocity.app.VelocityEngine;
org.apache.velocity.Template;
org.apache.velocity.VelocityContext;
/**
* Velocity "Hello, World" program.
*/
public class HelloWorld
{
public static void main( String[] args )
throws Exception {
// Get and initialize a Velocity engine.
VelocityEngine ve = new VelocityEngine();
ve.init();
Java Server Technologies
Introduction To Velocity
10
Bob Tarr
11
Bob Tarr
javac HelloWorld.java
Welcome to Velocity!
java HelloWorld
Output:
Hello World!
Welcome to Velocity!
Introduction To Velocity
12
Bob Tarr
References
l
Variable
Property
Method
Introduction To Velocity
13
Bob Tarr
Variable Reference
l
Syntax:
$foo
${foo}
Introduction To Velocity
14
Bob Tarr
Property Reference
l
Syntax:
$foo.bar
${foo.bar}
l
l
Note that the value returned could be any object, not just a String
If the value of foo in the context is a bean, return the value of
bean.getBar(). (This is an implied Method Reference.)
Introduction To Velocity
15
Bob Tarr
Method Reference
l
Syntax:
$foo.getTitle()
$foo.setName(Fred)
${foo.getTitle()}
${foo.setName(Fred)}
Look up the value of the foo in the context. Then call the
method on the returned object.
Again note that if the returned object is a bean, the Property
reference $foo.bar is the same as the Method Reference
$foo.getBar()
But you can only pass parameters to methods using Method
References
Introduction To Velocity
16
Bob Tarr
Quiet Reference
l
What if the reference is not found in the context? Then the literal
context string is output:
$foo can not be found in the context, so
$foo replaced by $foo
Introduction To Velocity
17
Bob Tarr
Set Directive
l
Introduction To Velocity
18
Bob Tarr
If/ElseIf/Else Directives
l
l
Typical conditional
Example:
#if($foo)
True Part
#else
False Part
#end
Introduction To Velocity
19
Bob Tarr
Foreach Directive
l
l
Loop construct
Example:
#foreach($product in $allProducts)
$product
#end
Introduction To Velocity
20
Bob Tarr
10
Include Directive
l
l
l
Includes a local file which is inserted into the location where the
#include directive is defined
The contents of the file are not processed by the template engine
Example:
#include (Banner.txt)
#include ($header)
#include ($footer)
Introduction To Velocity
21
Bob Tarr
Parse Directive
l
l
l
Introduction To Velocity
22
Bob Tarr
11
Introduction To Velocity
23
Bob Tarr
Introduction To Velocity
24
Bob Tarr
12
Introduction To Velocity
25
Bob Tarr
Introduction To Velocity
26
Bob Tarr
13
Run:
Output:
Books for Sale!
We have the following 3 books
for sale today:
Java Rulez by Bob Tarr for only $44.95
Dave Barry Talks Back by Dave Barry for only $22.00
The Joy Of Work by Scott Adams for only $13.99
Buy Today!
Introduction To Velocity
27
Bob Tarr
<HTML>
<HEAD>
<TITLE>Books For Sale!</TITLE>
</HEAD>
<BODY>
<CENTER>
<BR/>We have the following $bookList.size() books
for sale today:
#set($count = 1)
<TABLE>
#foreach($book in $bookList)
<TR>
<TD>$count)</TD>
<TD>$book.title</TD>
Java Server Technologies
Introduction To Velocity
28
Bob Tarr
14
Introduction To Velocity
29
Bob Tarr
Run:
Output:
<HTML>
...
We have the following 3 books for sale today:
<TABLE>
<TR>
<TD>1)</TD>
<TD>Java Rulez</TD>
<TD>Bob Tarr</TD>
<TD>$44.95</TD>
</TR>
...
Introduction To Velocity
30
Bob Tarr
15
Introduction To Velocity
31
Bob Tarr
Introduction To Velocity
32
Bob Tarr
16
Introduction To Velocity
33
Bob Tarr
Introduction To Velocity
34
Bob Tarr
17
Introduction To Velocity
35
Bob Tarr
Introduction To Velocity
36
Bob Tarr
18
Introduction To Velocity
37
Bob Tarr
Introduction To Velocity
38
Bob Tarr
19
Introduction To Velocity
39
Bob Tarr
Introduction To Velocity
40
Bob Tarr
20