<s1 title="Velocity-Style Syntax">

<p>The Apache <a href="http://jakarta.apache.org/velocity">Velocity</a>
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.
</p>

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

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

<example title='JSP style'>
&lt;%
int count;
%>

&lt;h3>A sample &lt;%= count %>&lt;/h3>

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

<p>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.</p>

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

&lt;h3>A sample ${count}&lt;/h3>

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

<p>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.</p>

<s2 title="Enabling velocity-style syntax">

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

<example title='Enabling velocity for a web-app'>
&lt;web-app>
  &lt;jsp velocity='true'/>
  ...
&lt;/web-app>
</example>

</s2>

<s2 title='expressions'>

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

<p>The '${...}' syntax is equivalent to '<var>&lt;c:out value="..."/></var>'.</p>

<def>
${<var/expression/>}
</def>

</s2>

<s2 title='scriptlets'>

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

<def>
#{
 <var/statements/>
}#
</def>

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

</s2>

<s2 title='if statements'>

<p>The velocity-style syntax directly supports if statements.  The syntax is</p>

<def>
#if (expr1)
  ...
#elseif (expr1)
  ...
#else
  ...
#end
</def>

<p>The expressions can be any JSTL expression.  The if statement is transformed into:</p>

<example>
&lt;c:choose>
&lt;c:when test="${<var/expr1/>}">
  ...
&lt;/c:when>
&lt;c:when test="${<var/expr2/>}">
  ...
&lt;/c:when>
&lt;c:otherwise>
  ...
&lt;/c:otherwise>
&lt;/c:choose>
</example>

</s2>

<s2 title='foreach statements'>

<p>The velocity-style syntax directly supports iteration with a foreach
statements.</p>

<def>
#foreach (var in expr)
  ...
#end
</def>

<p>This style of foreach is transformed into the following:

<example>
&lt;c:forEach items="${<var/expr/>}" var="<var/var/>">
  ...
&lt;/c:forEach>
</example>

<p>An example use might be the following:</p>

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

<p>The velocity-style syntax also supports integer iteration.</p>

<def>
#foreach (var in [begin .. end])
  ...
#end
</def>

<p>The range is transformed into the following:</p>

<example>
&lt;c:forEach begin="${<var/begin/>}" end="${<var/end/>}" var="<var/var/>">
  ...
&lt;/c:forEach>
</example>

<p>An example might be the following:</p>

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

</s2>
</s1>
