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. The syntax is based on expressions like 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.
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.
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 wit or in the web-app with the <jsp> tag:
Expressions are enclosed between "${" and "}", for example '${count}' and '${count + 15}'. The '${...}' syntax is equivalent to ' '.
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.)
The velocity-style syntax directly supports if statements. The syntax is
The expressions can be any JSTL expression. The if statement is transformed into:
The velocity-style syntax directly supports iteration with a foreach statements.
This style of foreach is transformed into the following:
An example use might be the following:
The velocity-style syntax also supports integer iteration.
The range is transformed into the following:
An example might be the following:
|