Notes
Slide Show
Outline
1
Apache Velocity
The JSP Killer?
  • A Java-based template engine with Apache Struts integration
2
Velocity the JSP Killer?
  • What is Velocity? What can Velocity do?
  • How does Velocity fits into the MVC design pattern (Model-2 Web Applications)?
  • Velocity Template Language (VTL)
  • How does Velocity integrates with Struts?
  • Velocimacros (VTL) instead of Taglibs.
  • View Tools (Java classes) instead of Taglibs.
  • Why Velocity instead of JSP (technical, philosophical, business and practical reasons)?
  • Example of comparing with JSP (WebEval3)
  • JSP-Velocity conversion tips
  • On-line Velocity resources
  • Summary
3
What is Velocity
  • Excerpt from http://jakarta.apache.org/velocity/index.html:


  • Velocity is a Java-based template engine. It permits anyone to use the simple yet powerful template language to reference objects defined in Java code. When Velocity is used for web development, Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, ... Velocity separates Java code from the web pages, making the web site more maintainable over the long run and providing a viable alternative to Java Server Pages (JSPs) or PHP. …


4
What can Velocity do?
  • Mail merging (email generation),
  • Generate source code and reports
  • Generate SQL
  • Generate XML
  • Generate Postscript and Rich Text
  • Embed into other systems as an integrated component


5
Velocity and MVC design pattern
6
Velocity Template Language
  • ## Single line comment
  • #*
  • Multiple line comments
  • *#


  • #set ($var = “$interpretedExpression”)
  • #set ($var = ‘$literalExpression’)


  • #if (($var == $var2) && ($var > $var1))
  • #else
  • #elseif
  • #end


  • #foreach ($obj in $objects)
  • #end


  • #include(“single.vm multiple.vm”) ## Static include
  • #parse(“onlySingle.vm”) ## Dynamic parsing


  • #stop


  • #macro (macro_name optionalArg1 optionalArg2 …)
  • #end
7
How does Velocity integrates with Struts?
  • VelocityStruts is a subproject of Velocity that aims to integrate Velocity with the Jakarta Struts project. The main java class is the VelocityViewServlet which handles *.vm files within a web app through mappings defined in the web.xml file.
  • Velocity templates (files with extension .vm) are nothing but markups (typically HTML) with 2 Velocity extensions:
    • References (begins with $) such as $user.name
    • Directives (begins with #) such as #set($name = $user.name)
  • It includes mechanism to access standard Servlet API objects - $request, $session, $application, and $response.
  • It includes mechanism to access Struts resources - ErrorsTool, FormTool, LinkTool, MessageTool.
  • Tools (or View Tools) are java class loaded through configuring file ‘toolbox.xml’ or loaded dynamically through the ToolLoader tool.
  • VelocityStruts is configured through ‘velocity.properties’, ‘web.xml’ files.
8
How does Velocity integrates with Struts?
  • Velocity really shines when it comes to accessing application data from within the templates. It handles access to objects and their methods. Object scope is automatically handled by Velocity in the search order of request, session and application.
9
Velocimacros (VTL) instead of Taglibs
  • Velocity Template Language (VTL) allows setting up velocimacros to replace Taglibs.
  • Velocimacros can be defined in-line within any .vm files or it can be defined within a library file loaded when velocity starts up (velocity.properties).
10
View Tools (Java classes) instead of Taglibs
  • Velocity can also be extended with “View Tool” which is Java class with public methods. This feature also replaces the need for Taglibs.
  • Bundled view tools – ErrorsTool, FormTool, LinkTool, MessageTool, DateTool, MathTool, ParameterParser, ToolLoader.


11
Why Velocity instead of JSP?
  • Technical – JSP is compiled, Velocity is interpreted which is easier to troubleshoot.
  • Philosophical – JSP “pollutes” MVC with page directives such as imports and cache size. It also pollutes MVC because page designer has to be aware of concept of data scoping (request, session, application).
  • Business – It is less costly to hire layout engineer than with Java engineer. It cuts development time since layout engineer are familiar with scripting language such as PHP.
  • Practical – Layout engineer can begin work with java engineer in parallel and merge whenever the java object structure is ready with very little modifications to template files.
12
Example of comparing with JSP (WebEval3)
  • (Lights on, go back to handout)
13
JSP-Velocity conversion tips
  • HTML special character sequence escaping (filtering)
        • Default is not to filter special characters (‘<‘, ‘>’…)
        • #set($tagBegin = “<font color=‘red’>”)
        • #set($tagEnd = “</font>”)
        • <html> … ${tagBegin}Red color here!${tagEnd} … </html>
        • Use view tool to escape variables ($xmlTool.getText($myVar))
        • Intercept Velocity variable substitutions to perform filtering
  • Dollar sign escaping
        • #set($myString = “\$myString”)
  • Pound sign escaping
        • #set($poundSign = “#”)
  • Single quote evaluation
        • #set($test = “Ha Ha”)
        • #set($myNewStr = “$test”) ## will evaluate (Ha Ha)
        • #set($myNewStr = ‘$test’) ## will not evaluate ($test)
        • #set($myNewStr = “Funny ‘$test’ !”) ## will evaluate (Funny ‘Ha Ha’ !)
14
On-line Velocity resources
  • http://jakarta.apache.org/velocity
  • http://www.teamup.com/jakarta-velocity-tools/docs/index.html
  • http://quadstar.psc.sc.edu/velstruts


15
Summary
  • What Velocity is and what it can do.
  • Velocity fits into Model View Controller design pattern.
  • Velocity Template Language (VTL)
  • How Velocity integrates with Struts.
  • Velocimacros and View Tools instead of Taglibs.
  • Reasons for using Velocity instead of JSPs.
  • WebEval3 example
  • JSP-Velocity conversion tips
  • On-line Velocity resources
16
Apache Velocity
The JSP Killer?
  • A Java-based template engine with Apache Struts integration