榪欐槸鍏充簬Velocity妯$増璇█鐨勬寚鍗椼傝寰楀埌鏇村鐨勪俊鎭紝璇峰弬闃匳elocity 鐢ㄦ埛鎵嬪唽銆俆his guide is the reference for the Velocity Template Language (VTL). For more information, please also refer to the Velocity User Guide.
鍚堟硶鐨勫彉閲忚鍒欙細Notation:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
渚嬪瓙錛欵xamples:
鍚堟硶鐨勮鍒欙細Notation:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, ]* [ } ]
渚嬪瓙 Examples:
鍚堟硶鐨勮鍒橬otation:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, ]*( [ opional parameter list... ] ) [ } ]
渚嬪瓙錛欵xamples:
鏍煎紡錛欶ormat:
#set( $*ref *= [ ", ' ]arg[ ", ' ] )
鍏朵腑錛歎sage:
渚嬪瓙錛欵xamples:
鍙蟲搷浣滄暟涔熷彲浠ユ槸綆鍗曠殑鏁板琛ㄨ揪寮忥紝姣斿錛歍he RHS can also be a simple arithmetic expression, such as:
鏍煎紡錛欶ormat:
#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end
鍏朵腑錛歎sage:
渚嬪瓙錛欵xamples:
鏍煎紡錛欶ormat:
#foreach( $ref in arg ) statement #end
鍏朵腑錛歎sage:
涓嬮潰鏄渷鐣ヤ簡澶勭悊璇彞鐨?foreach()鐨勪緥瀛愶細Examples of the #foreach(), omitting the statement block :
Velocity鎻愪緵浜嗕竴涓畝鍗曠殑寰楀埌寰幆嬈℃暟鐨勬柟娉曪紝鎵浠ヤ綘鍙互鍍忎笅闈㈣繖鏍鋒搷浣滐細Velocity provides an easy way to get the loop counter so that you can do something like the following:
<table>
#foreach( $customer in $customerList )
<tr>
<td>$velocityCount</td>
<td>$customer.Name</td>
</tr>
#end
</table>
榛樿鐨勮鏁扮殑鍙橀噺鏄?velocityCount錛屼笉榪囷紝浣犱篃鑳藉湪velocity.properties鏂囦歡涓繘琛岃嚜宸辯殑閰嶇疆銆傞粯璁ょ殑鎯呭喌涓嬶紝璁℃暟鍣ㄦ槸浠?寮濮嬬殑錛屼絾鏄繖涔熷彲浠ュ湪velocity.properties鏂囦歡涓繘琛岄厤緗紝涔熻兘浠?寮濮嬭鏁般備笅闈㈡槸鍦╲elocity.properties鏂囦歡涓璁℃暟鍣ㄧ殑鍚嶅瓧鍜岃搗濮嬪奸厤緗殑浠g爜鐗囨柇錛歍he default name for the loop counter variable reference, which is specified in the velocity.properties file, is $velocityCount. By default the counter starts at 1, but this can be set to either 0 or 1 in the velocity.properties file. Here's what the loop counter properties section of the velocity.properties file appears:
# Default name of the loop counter
# variable refernce.
counter.name = velocityCount
# Default starting value of the loop
# counter variable reference.
counter.initial.value = 1
鏍煎紡錛欶ormat:
#include( arg[, arg2, ... argn] )
渚嬪瓙錛欵xamples:
浠涔堝彨涓嶅姞瑙i噴?灝辨槸璇磛elocity鍙槸鎶婅繖涓枃浠剁殑鍐呭鐩存帴鍚堟垚鍒版寚瀹氱殑浣嶇疆錛岃屽鏋滃湪榪欎釜鏂囦歡涓篃鏈塚TL鐨勮鍙ワ紝鏄笉浼氱炕璇戠殑銆傚鏋滆瀹炵幇榪欎釜鍔熻兘錛岃鐢ㄥ埌涓嬮潰鐨?parse銆?/p>
鏍煎紡錛欶ormat:
#parse( arg )
渚嬪瓙錛欵xamples:
榪欓噷鍏佽閫掑綊鐨勮皟鐢ㄣ備綘鍙互鍦╲elocity.properties涓敼鍙?em>parse_directive.maxdepth 鐨勫兼潵紜畾鍏佽閫掑綊鐨勫眰鏁般傦紙榛樿鐨勫間負10錛塕ecursion permitted. See parse_directive.maxdepth in velocity.properties to change from parse depth. (The default parse depth is 10.)
鏍煎紡錛欶ormat:
#stop
Usage:
榪欎釜鏂規硶鑳藉仠姝㈡墽琛屽綋鍓嶇殑妯$増錛屽緢閫傚悎鐢ㄤ簬DEBUG銆俆his will stop execution of the current template. This is good for debugging a template.
鏍煎紡錛欶ormat:
#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] #end
濡傛灉瀹忎竴鏃﹀畾涔夛紝灝卞彲浠ュ儚涓嬮潰榪欐牱璋冪敤錛歄nce defined, the VM is used like any other VTL directive in a template.
#vmname( $arg1 $arg2 )
VM鍙互鍦ㄤ笅闈袱涓湴鏂瑰畾涔夛細VMs can be defined in one of two places:
琚敞閲婁簡鐨勫唴瀹逛笉浼氳鍚堟垚銆侰omments are not rendered at runtime.
Example:
## This is a comment.
Example:
#*
This is a multiline comment.
This is the second line
*#
Velocity鍦ㄥ簲鐢ㄤ腑閫氬父鐢ㄤ簬鐢熸垚欏甸潰錛屾潵鐩存帴浠f浛JSP銆備竴浜涗嬌鐢╒elocity鏉ョ敓鎴怶eb欏甸潰鐨勫ソ澶勫涓嬶細Velocity is often used to generate web pages in applications, usually as a direct replacement for JSP. Some of the benefits of using Velocity to generate web pages are:
榪欑瘒鏂囨。涓彁渚涗簡涓浜涘皢Velocity鐢ㄤ簬Web搴旂敤涓殑鍩烘湰淇℃伅銆俆his document provides some basic info on getting started with Velocity in a web application.
Velocity寮曟搸鐨勪富瑕佸姛鑳芥槸鍩轟簬涓涓ā鐗堢敓鎴愭枃鏈傚洜姝わ紝Velocity騫舵病鏈夊寘鍚換浣曚笌Web鐩稿叧鐨勫姛鑳姐俆he primary purpose of the Velocity engine is simply to generate text based on a template. Consequently, Velocity does not contain any web-related functionality in and of itself. 瑕佸垱寤轟竴涓猈eb搴旂敤錛屼綘闇瑕佷竴涓鏋舵潵鍝嶅簲HTTP璇鋒眰錛屽鐞嗙敤鎴瘋璇侊紝璋冪敤涓氬姟閫昏緫錛屽茍涓旇繑鍥炲搷搴斻俆o make a web application, you will need a framework to respond to HTTP requests, handle user authentication, make business logic calls, and generate a response. 涓嬮潰鏄竴浜涢潪甯稿己澶х殑鎻愪緵鑰呫俆here are several strong contenders.
欏轟究涓鎻愶紝浣犲彲鑳藉伓鐒跺彂鐜?span class=nobr>VelocityServlet榪欎釜綾伙紝璇ュ璞℃槸涓涓湪Velocity 1.4鐗堟湰涓笉鎻愬′嬌鐢ㄧ殑servlet銆?span class=nobr>VelocityServlet
宸茬粡涓嶅啀緇存姢錛屾墍浠ワ紝鎴戜滑寮虹儓寤鴻浣犱嬌鐢╒elocity Tools涓殑VelocityViewServlet銆侫s a side note, you may also come across references to VelocityServlet
, which is a deprecated servlet that was included in the Velocity Engine up to version 1.4. Since VelocityServlet
is no longer being maintained we strongly recommend you use VelocityViewServlet in Velocity Tools instead.
瀵逛簬Velocity鍦╳eb鐨勫簲鐢紝鏈変竴浜涢棶棰橀渶瑕佽璁恒傝繖浜涢兘鏄粡甯擱亣鍒扮殑闂涓殑涓浜涚畝鍗曠殑璁ㄨ銆俆here are a few issues with Velocity that are specific to web applications. Here is a brief discussion of the most commonly encountered issues.
鍦╒elocity涓紝濡傛灉灝嗕竴涓璞′綔涓轟竴涓紩鐢紝閭d箞Velocity鎻愪緵浜嗚闂瀵硅薄鐨勪換浣曟柟娉曠殑鑳藉姏銆傚湪欏甸潰涓樉紺轟俊鎭殑鏃跺欙紝榪欎釜鍔熻兘寰堟湁鐢紝浣嗘槸鍚屾椂濡傛灉瀵硅薄鎴栬呭簲鐢ㄧ殑鐘舵佽鏀瑰彉浜嗭紝榪欐牱鍋氫篃鏄緢鍗遍櫓鐨勩俈elocity provides the ability to call any method of an object acting as a reference. This can be useful when displaying information into the page but is dangerous when object or application state is modified.
涓句釜渚嬪瓙錛屼笅闈㈢殑浠g爜瀹夊叏鐨勮皟鐢ㄤ簡涓涓猯ist鐨剆ize()鏂規硶騫舵樉紺虹粨鏋溿侳or example, the following code safely calls the size() method of a list and displays the result.
There are $users.size() currently logged in.
涓涓笉瀹夊叏璋冪敤鐨勪緥瀛愶細鍦ㄤ竴涓儲鍔eb欏甸潰涓婏紝鍦ㄤ笂涓嬫枃涓殑涓涓璞¤兘璁$畻姣忎竴騫寸殑鏁版嵁銆傛柟娉昪alculateNextYear()鏂規硶鑳借綆椾笅涓騫寸殑鏁版嵁騫跺皢涓涓唴閮ㄧ殑璁℃暟鍣ㄥ仛澧炲姞鎿嶄綔銆侫n example of an unsafe operation concerns a financial web page with an object in the context that calculates data year by year. The method calculateNextYear() calculates data for the next year and advances an internal counter:
2005 data: $table.data
$table.calculateNextYear()
2006 data: $table.data
The problem with this approach is that the code cannot be repeated in multiple parts of the page. You may not intend to do so, but it's easy to forget this when cutting and pasting or writing control statements (such as #if or #foreach). This becomes more of an issue when you are dealing with application or session-level state.
鎴戜滑寮虹儓寤鴻鐨勫疄璺墊槸鍙嬌鐢╒elocity鍚戞枃鏈腑鎻掑叆淇℃伅銆傛柟娉曡皟鐢ㄤ細鏄緱鍒頒俊鎭殑涓涓湁鏁堢殑鏂規硶錛屼絾涓鑸鏉ヤ嬌鐢ㄤ竴涓柟娉曡皟鐢ㄦ潵鏀瑰彉瀵硅薄鐨勭姸鎬佹槸涓涓笉鑹殑瑙傚康錛屾潵鏀瑰彉搴旂敤鐨勭姸鎬佷竴瀹氭槸涓涓敊璇殑鎯蟲硶銆俆he (strongly) recommended practice is to only use Velocity for inserting information into text. Method calls can be useful to retrieve information. However, it's generally a bad idea to use a method call to change object state, and it's always a bad idea to change application state.
濡傛灉浣犲繀闇瑕佹敼鍙樺璞$殑鐘舵侊紙姣斿鍓嶉潰鐨勪緥瀛愶級錛屽彲浠ラ鍏堣綆楀嚭鎵鏈夊彲鑳界殑鍊煎茍鏀懼叆涓涓狶ist鎴栧垯Map瀵硅薄涓傛墍鏈夌殑瑕佹敼鍙樺簲鐢ㄧ殑鐘舵佺殑鍔ㄤ綔搴旇鎬繪槸鏀懼湪鎺у埗鍣ㄤ腑銆侷f you find yourself needing to change object state (as in the previous example) try precalculating all the possible values in the controller and putting them in a List or Map. Any changes to application state should always be done by the controller.
涓涓浉鍏崇殑娉ㄦ剰錛屼綘搴旇鍦ㄤ笂涓嬫枃瀵硅薄涓斁鍏ヤ竴涓狶ist鎴栬呬竴涓猄et瀵硅薄錛岃屼笉鏄斁鍏ヤ竴涓狪terator鎴栬匛numeration銆傝繖鏍峰厑璁擱泦鍚堝璞″湪娌℃湁鏀瑰彉鏃墮噸澶嶄嬌鐢ㄣ侽n a related note, you should always put a List or Set into the context instead of an Iterator or Enumeration. This allows the collection to be used more than once in the page with no change in behavior.
浠諱綍鐢ㄦ埛娣誨姞鐨勫甫鏈塇TML鎴栬匵ML鏍囪錛堟瘮濡?lt;,>,&錛夌殑鏂囨湰閮藉簲璇ュ湪濉埌web欏甸潰涔嬪墠琚幓鎺夈備負浜嗙‘淇濋〉闈㈡紜樉紺哄拰閬垮厤鍗遍櫓鐨勮法绔欑偣鑴氭湰錛坈ross-site scripting涓縐嶆敾鍑繪墜孌碉紝鍦ㄩ〉闈腑鍖呭惈鍙︿竴涓甫鏈夋湪椹殑欏甸潰錛夈備笉璞STL錛圝ava鏍囧噯鏍囪璇█錛夛紝Velocity涓嶅寘鍚幓闄TML鏍囪鐨勫姛鑳斤紝浣嗘槸浣犲彲浠ラ氳繃涓夌鏂規硶鏉ヨ揪鍒扮洰鐨勩侫ny user-entered text that contains special HTML or XML entities (such as <, >, or &) needs to be escaped before included in the web page. This is required, both to ensure the text is visible, and also to prevent dangerous cross-site scripting. Unlike, for example, JSTL (the Java Standard Tag Language found in Java Server Pages), Velocity does not contain any HTML-specific escaping functionality. However, you have three options:
娉ㄦ剰錛屾湁鏃跺欏叾浠栫殑鏍囪涔熼渶瑕佽鍘繪帀銆傛瘮濡傚湪CSS閲岄潰錛孈闇瑕佽鍘繪帀錛屽湪Javascript錛屽崟鐙殑'絎﹀彿闇瑕佷粠瀛楃涓蹭腑鍘繪帀鎴栨浛鎹€侼ote that other kinds of escaping are sometimes required. For example, in style sheets the @ character needs to be escaped, and in Javascript strings the single apostrophe ' needs to be escaped.
褰撲竴涓獁eb搴旂敤鍦ㄦ湇鍔″櫒涓婂紑濮嬭繍琛屼簡錛屾湁浜嗚嫢騫茬殑鐢ㄦ埛鍜屼竴浜涘彈淇濇姢鐨勮祫婧愶紝榪欐椂鍊欙紝搴旂敤鐨勫畨鍏ㄦф柟闈㈢殑瑕佹眰灝辨樉鐜板嚭鏉ヤ簡銆俈elocity涓凡緇忔兜鐩栦簡澶у鏁版爣鍑嗙殑web瀹夊叏鎬ф爣鍑嗐備竴浜涚壒鍒殑闂錛堟瘮濡傜郴緇熼厤緗紝璺ㄧ珯鐐硅剼鏈紝鎴栬呮柟娉曡嚜鐪侊級絳夐兘鍦˙uilding Secure Applications with Velocity 榪欑瘒鏂囩珷涓婃湁鎵浠嬬粛銆係ince a web application is running on a central server, that typically has multiple users and confidential resources, care must be taken to make certain that the web application is secure. Most standard web security principles apply to a web application built with Velocity. A few specific issues (such as system configuration, more on cross-site scripting, and method introspection) are written up in this article on Building Secure Applications with Velocity.
榪樻湁涓鐐瑰氨鏄疺elocity鍦ㄦ棩蹇楁柟闈㈢殑閰嶇疆姣旇緝緙轟箯銆備粬鎬繪槸鍦ㄥ綋鍓嶆枃浠跺す涓垱寤轟竴涓棩蹇楁枃浠躲傚綋Velocity鐢ㄥ湪Web搴旂敤涓殑鏃跺欙紝褰撳墠鏂囦歡澶規寚鐨勬槸搴旂敤寮濮嬬殑閭d釜鏂囦歡澶廣傚鏋滀綘鍙戠幇"velocity.log"榪欎釜鏂囦歡鍑虹幇鍦ㄤ綘鏂囦歡緋葷粺鐨勪笉鍥哄畾浣嶇疆錛岃媯鏌ヤ綘鐨刅elocity 鏃ュ織鐨勯厤緗備竴鑸儏鍐典笅錛屽嚭鐜拌繖涓棶棰樼殑鍘熷洜鏄湪闄や簡欏甸潰鐢熸垚涔嬪鐨勫叾浠栧湴鏂硅繕浣跨敤浜哣elocity錛堟瘮濡傚彂閫侀偖浠訛級銆侫 minor point is that Velocity, in the absence of any log-related configuration, creates a log file in the current directory. When Velocity is used in a web application the "current directory" is usually the current directory from which the application server is started. If you start seeing the file "velocity.log" files in random places on your server filesystem, check the Velocity log configuration. Typically this occurs when Velocity is used within a web application outside of web page generation (e.g. for sending email).
涓嬮潰鏄竴涓粙緇嶄嬌鐢?a title=VelocityViewServlet >VelocityViewServlet鏉ュ緩绔嬩竴涓畝鍗曠殑web搴旂敤鐨勭畝瑕佹暀紼嬨傛洿澶氱殑淇℃伅璇峰弬瑙乂elocity Tools 鐨勬枃妗c俉hat follows is a brief tutorial on building a simple web app with VelocityViewServlet. For more information, consult the Velocity Tools documentation.