📄 velocity.xtp
字号:
<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 JSPexpressions and scriptlets. Resin's extension allows the useof Velocity-style syntax in JSP files. The Velocity-style syntaxis transformed into JSTL standard tags.</p><p>The syntax is based on expressions like <var/${foo}/>and scriptlets with <var/#{...}#/>. Because thealternative syntax avoids the brackets which fill JSP pages, it can makepages more readable and therefore more maintainable.</p><p>Because Resin's Velocity-style syntax is transformed to the JSTLtag library, all JSTL expressions are allowed.</p><example title='JSP style'><%int count;%><h3>A sample <%= count %></h3><% if ("foo".equals(request.getParameter("a"))) { %> <h3>Foo!</h3><% } else { %> <h3>Bar!</h3><% } %></example><p>The same JSP file could be written in Velocity-style as follows. Thejsp:directive is required because JSP pages use strict JSP syntaxby default.</p><example title='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</example><p>The choice between the two is a matter of preferences. An advantageof the velocity style is that expressions and scriptlets avoid usingbrackets. In large pages, sorting out the HTML or XML from the JSP syntaxcan 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 <jsp> tag:</p><example title='Enabling velocity for a web-app'><web-app> <jsp velocity='true'/> ...</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><c:out value="..."/></var>'.</p><def>${<var/expression/>}</def></s2><s2 title='scriptlets'><p>Scriptlets use the '#{ ... }#' syntax. This is entirely equivalent to'<% ... %>'. (Note, Velocity does not have this syntax because itcreates 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><c:choose><c:when test="${<var/expr1/>}"> ...</c:when><c:when test="${<var/expr2/>}"> ...</c:when><c:otherwise> ...</c:otherwise></c:choose></example></s2><s2 title='foreach statements'><p>The velocity-style syntax directly supports iteration with a foreachstatements.</p><def>#foreach (var in expr) ...#end</def><p>This style of foreach is transformed into the following:<example><c:forEach items="${<var/expr/>}" var="<var/var/>"> ...</c:forEach></example><p>An example use might be the following:</p><example title='foreach in Java'><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) <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><c:forEach begin="${<var/begin/>}" end="${<var/end/>}" var="<var/var/>"> ...</c:forEach></example><p>An example might be the following:</p><example title='foreach in Java'><jsp:page velocity='true'/>#foreach (value in [3..9]) <li>$value#end</example></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -