<document>

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.

<% 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.

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

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

<jsp:directive.page isVelocityEnabled='true'/> ... <web-app xmlns="http://caucho.com/ns/resin"> <jsp velocity-enabled='true'/> ... </web-app>

Resin uses the format of the web.xml to determine the specification version for the web application. Older versions of the specification did not support JSTL syntax, which is also used for Velocity syntax (see expressions, below). It is important when using velocity syntax that the WEB-INF/web.xml and/or WEB-INF/resin-web.xml files indicate the proper specification version. The easiest way to do this is to specify the top level element as `<web-app xmlns="http://caucho.com/ns/resin">`

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

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

<def> ${'${'}} </def>

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.)

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

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

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

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

<example> <c:choose> <c:when test="${'${'}}"> ... </c:when> <c:when test="${'${'}}"> ... </c:when> <c:otherwise> ... </c:otherwise> </c:choose>

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

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

This style of foreach is transformed into the following:

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

An example use might be the following:

<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:

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