Apache Velocity Engine

Markus Auchmann [email protected]

Schedule

• Fundamentals • Velocity Introduction • Velocity Hello World! • Velocity Template Language • Sample Applications • Summary

Slide 2 Apache Velocity Engine Fundamentals

Template Engine (1/2)

A template engine is a software or a software component that is designed to combine one or more templates with a data model to produce one or more result documents.

“A template engine is a code generator that emits text using templates embedded with actions or data references. Engines generally espouse a model-view pattern in an attempt to separate the data source and template..” [Antl06]

Slide 4 Apache Velocity Engine Template Engine (2/2)

Elements: • Data Model • Source Template • Template Engine • Result Document

Slide 5 Apache Velocity Engine

Model View Controller

Task: “ … separate core business model functionality from the presentation and control logic”

Slide 6 Apache Velocity Engine Web Template Engine

Web template engines are designed to produce web pages or web documents to be delivered over the internet.

Slide 7 Apache Velocity Engine

Summary

Goals of a (Web) Template Engine: • Separation of application code from the presentation • Avoid breaking of application by designers • Template lets designer focus on presentation • Advantages of the Model View Controller

Slide 8 Apache Velocity Engine Velocity Introduction

History

• First version March 2001 • Many releases • Latest stable Version is 1.4 • Since October 2006 upgraded into an Apache Top Level Project (TLP) • Current beta Version 1.5 – beta2 • Final release of 1.5 upcoming soon

Slide 10 Apache Velocity Engine Features

“Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.” [Velo06]

• Clear separation between Model, View and Controller • Creation of Web Applications using servlets • Can generate output files in any format • Easy to use template language

Slide 11 Apache Velocity Engine

Fundamentals

Slide 12 Apache Velocity Engine Fundamentals – Context (1/3)

• Hashtable • Provides get and set methods • Accessible from the view and the controller

Slide 13 Apache Velocity Engine

Fundamentals – Context (2/3)

Hashtable:

Java.lang.String Java.lang.Object „Entry“ String(„ein Text“) „something“ Vector „Vector“ Array[]

Slide 14 Apache Velocity Engine Fundamentals – Context (3/3)

Methods: • boolean containsKey(java.lang.Object key) • java.lang.Object get(java.lang.String key) • java.lang.Object[] getKeys() • java.lang.Object put(java.lang.String key, - java.lang.Object value) • java.lang.Object remove(java.lang.Object key)

Slide 15 Apache Velocity Engine

Fundamentals – Controller

• Java Code • Access model • Access Context • Store information in Context • Provide methods for the Designer

Slide 16 Apache Velocity Engine Fundamentals – View

• Using the Velocity Template Language • Provide look and feel of the application • Use methods provided by the Controller • Use Context to retrieve and store information

Slide 17 Apache Velocity Engine

Fundamentals – Model

• Not part of Velocity itself • Used to make applications dynamic

Slide 18 Apache Velocity Engine Hello World!

Task

• Create a Hello World application • The text should be stored by the controller into the Context • Designer should retrieve this Object and output it

Slide 20 Apache Velocity Engine View

• Using the VTL

1 2 3 $hello 4 5

Slide 21 Apache Velocity Engine

Controller

• Create the Object

1 String hello = new String("Hello World!");

Slide 22 Apache Velocity Engine Velocity comes into play

Task: • Retrieve Template • Merge template and Context • Generate output

General Pattern

Slide 23 Apache Velocity Engine

1. Initialize Velocity

• Singleton Model • One instance of Velocity in the JVM

14 Velocity.init();

Slide 24 Apache Velocity Engine 2. Create Template

21 Template template = null; 22 try { 23 template = Velocity.getTemplate("helloworld.vm"); 24}

Slide 25 Apache Velocity Engine

3. Create Context

33 VelocityContext context = new VelocityContext();

Slide 26 Apache Velocity Engine 4. Put something into the Context

• Using the “put” method of the Context Object

36 context.put("hello", "Hello World!");

Slide 27 Apache Velocity Engine

5. Merge Template and Context

• Create a “writer” • Call the “merge” method • Pass the Context object and the output writer object • Merge is done (replacing VTL)

39 StringWriter writer = new StringWriter(); 40 41 try { 42 template.merge(context, writer); 43 }

Slide 28 Apache Velocity Engine 6. Show result

• Output is sent to the writer object • Use the “toString” method to output it to the console

48 System.out.println (writer.toString());

Slide 29 Apache Velocity Engine

Output

• Compile java Code • Execute

Slide 30 Apache Velocity Engine Summary

General Pattern: • Initialize Velocity • Create Template • Create Context • Put something into the Context • Merge Template and Context • Output

Slide 31 Apache Velocity Engine

Velocity Template Language Introduction

• Used by the Designer • (Should be) easy to use

Grouped into: • References • Directives • Velimacros

Slide 33 Apache Velocity Engine

References – Variables (1/2)

• Used to get values out of the Context

1 context.put("object", new Object()); 2 context.put("integer", new Integer(99)); 3 context.put("string", new String("String"));

Slide 34 Apache Velocity Engine References – Variables (2/2)

1 Object: 2 $object 3 Integer: 4 $integer 5 String: 6 $string

Slide 35 Apache Velocity Engine

References – Methods (1/2)

• Access to Objects provided by the Context

Friend -name -age +setName() +setAge() +getFriend()

1 context.put("friend", new Friend());

Slide 36 Apache Velocity Engine References – Methods (2/2)

1 Set the name of my friend ... 2 $friend.setName("Christoph Huber") 3 4 Set the age of my friend ... $friend.setAge(25) 5 6 $friend.getFriend()

Slide 37 Apache Velocity Engine

Directives – #set (1/2)

• Add values to the Context • Modify existing values

1 context.put("value", "a value added by java");

Slide 38 Apache Velocity Engine Directives – #set (2/2)

1 Before we use the set directive: 2 new value: $newValue 3 old value: $value 4 5 #set($newValue = "a new value added by VTL") 6 #set($value = "a value modified by VTL") 7 After the use of the set directive: 8 new value: $newValue 9 old value: $value

Slide 39 Apache Velocity Engine

Directives - Overview

• #set • #if • #else • #elseif • #foreach • #include • #parse • #stop

Slide 40 Apache Velocity Engine Velimacros – Overview

„The #macro script element allows template designers to define a repeated segment of a VTL template. Velocimacros are very useful in a wide range of scenarios both simple and complex.“ [VUse06]

• Usage: 1 #macro(hello $who) 2 Hello $who! 3 #end 4 5 #hello("Markus")

Slide 41 Apache Velocity Engine

Sample Applications XML Parsing

XML Parsing – Task

• Parse XML file and make it available in the Context

Slide 44 Apache Velocity Engine XML Parsing – Model

1 2 3 4 Christoph Huber 5 21 6 7 8 9 Niki Lauda 10 57 11 12 [...]

Slide 45 Apache Velocity Engine

XML Parsing – View

1 My friends are: 2 #foreach($element in $Vector) 3 #if("break" == $element) 4 5 #else 6 $element 7 #end 8 #end

Slide 46 Apache Velocity Engine XML Parsing – Controller

• Parse XML file with DOM • Read nodes and put it into the Vector • Separate entries by adding “break”

Slide 47 Apache Velocity Engine

Using BSF Overview

(BSF) is a set of Java classes which provides scripting language support within Java applications, and access to Java objects and methods from scripting languages.“ [JBSF06]

Covered in my thesis: • Jacl – Tcl • Rhino – JavaScript • BSF4Rexx - ObjectRexx

Slide 49 Apache Velocity Engine

oORexx to access the Model (1/2)

References: • Stefan Schmid: OpenOffice.org Database • Andreas Ahammer: OpenOffice.org Automation

Slide 50 Apache Velocity Engine oORexx to access the Model (2/2)

Task: • Query DB • Return result

Slide 51 Apache Velocity Engine

Output

Controller • Store result from oORexx into the Context

View • Output result

Slide 52 Apache Velocity Engine Velbook

Task

• Guestbook • Insert: name, mail, homepage, text • Name and text mandatory

• Using the VelocityViewServlet • ParameterParser • Tomcat • MySQL

Slide 54 Apache Velocity Engine Controller

• Java • Velocity

Slide 55 Apache Velocity Engine

View

• VTL • Designer

Slide 56 Apache Velocity Engine Model

• JDBC • MySQL • Java

Slide 57 Apache Velocity Engine

The Meeting

Slide 58 Apache Velocity Engine The Meeting Methods Ok! Collections

Slide 59 Apache Velocity Engine

The Meeting

@$”=)$/%=§ :(

Slide 60 Apache Velocity Engine The Meeting

Ok! Methods

Slide 61 Apache Velocity Engine

Velbook in Action Velocity powering Ajax

Introduction

• Generate every output file • Generate XML files

• To declare file as XML --> Velocity Properties

1 default.contentType = text/

Slide 64 Apache Velocity Engine Summary

Thanks for your attention Questions?