Velocity-Style Syntax
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Introduction
JSP Compilation
Reference
Tutorials
Articles
FAQ

JSP EL
JSTL
Directives
Variables
Actions
Applications
Velocity
Schema for JSP-2.0 .tld files
Applications
Reference
Schema for JSP-2.0 .tld files

The Apache Velocity project introduces an alternative syntax to the familiar JSP expressions and scriptlets. Resin's extension allows the use of Velocity-style syntax in JSP files. The Velocity-style syntax is transformed into JSTL standard tags.

  1. Enabling velocity-style syntax
  2. expressions
  3. scriptlets
  4. if statements
  5. foreach statements

The syntax is based on expressions like ${foo} and scriptlets with #\{...}#. Because the alternative syntax avoids the brackets which fill JSP pages, it can make pages more readable and therefore more maintainable.

Because Resin's Velocity-style syntax is transformed to the JSTL tag library, all JSTL expressions are allowed.

JSP style
<%
int count;
%>

<h3>A sample <%= count %></h3>

<% if ("foo".equals(request.getParameter("a"))) { %>
  <h3>Foo!</h3>
<% } else { %>
  <h3>Bar!</h3>
<% } %>

The same JSP file could be written in Velocity-style as follows. The jsp:directive is required because JSP pages use strict JSP syntax by default.

Velocity style
<jsp:directive.page velocity='true'/>
\#{
int count;
}#

<h3>A sample ${count}</h3>

#if ("foo" == params.a)
  <h3>Foo!</h3>
#else
  <h3>Bar!</h3>
#end

The choice between the two is a matter of preferences. An advantage of the velocity style is that expressions and scriptlets avoid using brackets. In large pages, sorting out the HTML or XML from the JSP syntax can become confusing.

Enabling velocity-style syntax

Velocity-style syntax can either be enabled on a per-JSP page with velocity='true' or in the web-app with the <jsp> tag:

Enabling velocity for a page
<jsp:directive.page velocity='true'/>
  ...

Enabling velocity for a web-app
<web-app>
  <jsp velocity='true'/>
  ...
</web-app>

expressions

Expressions are enclosed between "${" and "}", for example '${count}' and '${count + 15}'.

The '${...}' syntax is equivalent to '<c:out value="..."/>'.

\#{

${expression}

scriptlets

Scriptlets use the '\#{ ... }#' syntax. This is entirely equivalent to '<% ... %>'. (Note, Velocity does not have this syntax because it creates its own language instead of escaping to Java.)

\#{
 statements
}#

\#{
String key = request.getParameter("key");
if (key.equals("")) {
  response.sendError(500, "Bad key");
  return;
}
}#
...

if statements

The velocity-style syntax directly supports if statements. The syntax is

#if (expr1)
  ...
#elseif (expr1)
  ...
#else
  ...
#end

The expressions can be any JSTL expression. The if statement is transformed into:

<c:choose>
<c:when test="${expr1}">
  ...
</c:when>
<c:when test="${expr2}">
  ...
</c:when>
<c:otherwise>
  ...
</c:otherwise>
</c:choose>

foreach statements

The velocity-style syntax directly supports iteration with a foreach statements.

#foreach (var in expr)
  ...
#end

This style of foreach is transformed into the following:

<c:forEach items="${expr}" var="var">
  ...
</c:forEach>

An example use might be the following:

foreach in Java
<jsp:page.directive velocity='true' import='java.lang.*'/>
\#{
  ArrayList list = new ArrayList();
  list.add("foo");
  list.add("foobar");
  pageContext.setAttribute("list", list);
}#
#foreach (value in list)
  <li>${value}
#end

The velocity-style syntax also supports integer iteration.

An example might be the following:

foreach in Java
<jsp:page.directive velocity='true'/>
#foreach (value in [3..9])
  <li>$value
#end


Applications
Reference
Schema for JSP-2.0 .tld files
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.