Skip to main content

Templating with Velocity

Posted by daniel on February 18, 2004 at 7:32 AM PST

"For server-side applications, when integrated with Web tier containers compatible with Servlet 2.3+, Velocity provides a viable alternative to JSP technology that can enforce a clean separation of presentation logic from application business logic."

We link to Sing Li's developerWorks article on the Velocity Template engine in
Also in Java Today
. He begins with a standard mail merge type of example noting that such an "application is most useful when the data source being merged is large and varied." In many cases, the need just isn't great enough to take the step of creating a template for simple tasks. But, as you also saw in Erik Hatcher's article Velocity: Fast Track to Templating , what makes Velocity so powerful is that you can go from 0 to 60 pretty quickly.

Use of #if, #then, #else, and #foreach allows you to easily add selective rendering and repetition. So called velocimacros "enable you to encapsulate template scripts and reuse them easily." To import Java objects so that Velocity templates can access them you use the org.apache.velocity.context.Context.

Li concludes his article by comparing the use of Velocity on the server side with JSPs and shows you how to deploy Velocity with Tomcat 5. He cautions that "the JSP model provides unhindered access to the underlying Servlet API and Java programming language. In fact, to avoid access to these native features, you will have to exercise great discipline in coding (using only EL, tag libraries, and the like). Essentially, this is a wide-open access model." He continues that Velocity's "closed model allows Velocity to provide a decoupled templating presentation layer, cleanly separated from any application business logic or data management code."

Our other linked article is an overview of the new features you
now have access to in the J2SE 1.5 beta. In this Oracle Technology Network article, Jason Hunter previews the Big changes in J2SE 1.5. Written well before the current release, Jason describes Generics, covariant return types, autoboxing, enhanced for loop, varargs, static imports, and type safe enums. Also in today's Weblogs John Mitchell points to Gilad Bracha's Generics Tutorial.


In today's Projects and Communities The Java Communications homepage announces that JSR-22, the JAIN Service Logic Execution Environment API Specification has gone final.

People have started to publish tutorials in the Education and Research community. You can start with the Java Advanced Imaging examples and tutorial.


In today's java.net News Headlines
:

Registered users can submit news items for the href="http://today.java.net/today/news/">java.net News Page using
our news submission
form
. All submissions go through an editorial review by news director
Steve Mallet before being posted to the site. You can also subscribe to
thejava.net
News RSS feed
.


Current and upcoming
Java Events
:

Registered users can submit event listings for the href="http://www.java.net/events">java.net Events Page using our href="http://today.java.net/cs/user/create/e"> events submission
form. All submissions go through an editorial review before being
posted to the site.


Archives and Subscriptions: This blog is delivered weekdays as the
Java
Today RSS feed
. All java.net members can subscribe to the email
updates for the site at the href="https://java-net.dev.java.net/servlets/ProjectMailingListList">
java-net Mailing Lists page. You must be logged in to subscribe
to
the javanet_Daily and javanet_Weekly lists. Also, once this page
is no longer featured as the front page of
java.net
it will be archived along with other past issues in the href="http://today.java.net/today/archive/">java.net Archive.