Velocity
Velocity Three species of referencevariable: A string representation of a Java object that returns the result of invoking the ToString () method of Java. method: Call a method of an object that must be public, the return value is ToString (), and the parameter of the method must also be string. properties: Except for accessing Java's class properties, it is equivalent to get. () method.
Basic Symbols1, "#" to identify the velocity of the script statement. 2, "$" to identify an object (or variable). 3, "{}" is used to identify the velocity variable. 4, "! "To force the non-existent variable to appear blank. 5, in double quotation marks or single quotation marks, the default "", you can change the default processing mode in Stringliterals.interpolate=false
Basic Syntax 1. variables (1) Variable definition# ($name = "Hello")# ($templateName = "INDEX.VM")#set ($template = "$directoryRoot/$templateName")#template(2) Variable specification
${name} can also be written as $nameHowever, in general, use of ${name}Note: You cannot add {} When referencing a property(3) assigning values to variables$name = "Hello"Left:a reference to a variable or property. Right:variable references, literal strings, property references, method references, literal numbers, array lists.
#set ($monkey = $bill) # #变量引用#set ($monkey. Friend = "Monica") # # String Reference#set ($monkey. Blame = $spindoctory. Weave ($web) # # Method Reference#set ($monkey. Number = 123) # # Numeric Reference#set ($monkey. Say = ["not", $my, "Fault"] # #数组列表PS:1, the right value is null, the left side will not be assigned value, the original value will be preserved. 2. The velocity template is not defined and will be considered a string. #set ($foo = "gibbous")$moon = $foooutput is $moon=gibbous3. The velocity template will not be interpreted as an instance variable of an object. For example$foo. Getbar () is equivalent to $foo. Bar;$data. GetUser ("Jon") is equivalent to $data. User ("Jon");data.getrequest (). getServerName () is equivalent to $data. Request.servername is equivalent to $ (data. Request.servername)
2. Circulation#foreach ($element in $list)This is $element$velocityCount#endFor example:#set ($list =["pine", "oak", "maple"])#foreach ($element in $list)$velocityCountThis is $element .<br>#endthe output is: the $list can be vectors, Hashtable, and arrays. The value assigned to $element is a Java object and can be referenced by a variable. For example#foreach ($key in $list. KeySet)Key: $key--->value: $list. Get ($key) <br>#endVelocity also provides a method for looping the number of times, $velocityCouont the name of the variable is the Velocity default name, which indicates the loop to the first few times. For example#foreach ($foo in [1,2,3,4,5])$foo#end<br>#foreach ($bar in [2,1,0,-1,-2])$bar#end<br>#set ($arr =[0,1])#foreach ($i in $arr)$i#endThe output results are as follows: 3. Conditional Statements#if (condition) #elseif (condition)#else#endFor example:#set ($arr =["Jiayou", "JIAYOU2", "JIAYOU3"])#foreach ($element in $arr)#if ($velocityCount ==1)<div>jiayou</div>#elseif ($velocityCount ==2)<div>jiayou2</div>#else<div>jiayou3</div>#end#end
Output Result:
4. Statement nesting#foreach ($element in $list)#foreach ($element in $list)This is $element. $velocity <br>inner<br>#endThis is $element. $velocity <br>out<br>#end
5. CommentsLine Note: # #this is a singleMulti-line Comment: #* ... *#Document Format: #**.............*#
6. Relational and logical operators&& = = | |!#if ($foo && $bar)<strong> This and that</strong>#end
7. The macro in velocityA macro in velocity can be understood as a function. ① definition of macros#macro (macro name $ parameter 1 $ parameter 2 ...)statement body (i.e. function body)#end② calls to macros#宏的名称 ($ parameter 1 $ parameter 2 ...)Description: The parameters are separated by a space.
8. #stopstopping the execution of the template engine and returning it is helpful for debugging.
9, $include and #parse#include和 #parse's role is to introduce local files, in order to be safe, the files entered can only be in the Template_root directory. these two introduce differences:1) #include可以引入多个文件. Example: #include ("One.gif", "Two.txt", "three.html")#parse只能引入指定的单个对象. Example: #parse ("LAYOUT/INDEX.VM")2) #include引入的文件内容不会被velocity模板引擎解析. #parse引入的文件内容, the velocity is parsed and given to the template, which is equivalent to the copy of the introduced file into a file. 3) #parse是可以递归调用的. For example:called bycode in DOFOO.VM:Count Down.<br>#set ($count = 8)#parse (PARSEFOO.VM)<br>all done with Diao Yong Parsefoo.vm.
code in PARSEFOO.VM:$count$set ($count = $count-1)#if ($count >0) <br>#parse ("PARSEFOO.VM") # #自己调用自己#else<br> recursive call end#endOutput result : Note:when using #parse to nest another VM page, variable sharing issues such as:A.VM nested in the B.VMvariable $param was used in A.VM;The $param can be used directly in the B.VM.but if one of them is defined, it uses the same name variable that it defines itself.
10. Escape characterwhen a variable is defined, two \ \ represents a \, and if not defined, it is output as-is. For example:
11. Built-in ObjectsVelocity has built-in objects that can be called directly from the VM. For example$request, $response, $session, use the messaging tools within $MSG to access the international resources of struts and achieve a simple internationalization approach.
12. Array AccessThe array should be changed to a list and other container-like way to wrap.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(ii) The velocity script of the template engine has the basic syntax of all