Velocity, velocity. js

Source: Internet
Author: User

Velocity, velocity. js

  1. Velocity is a java-based template engine that allows anyone to reference objects defined by java code simply using the template language. As a relatively complete template engine, Velocity has powerful functions, but it also increases application complexity.
  2. I. Basic syntax
  3. 1. "#" is the script statement used to identify Velocity, including # set, # if, # else, # end, # foreach, # end, # iinclude, # parse, # macro, etc;
  4. For example:
  5. # If ($ info. imgs)
  6. # Else
  7. # End
  8. 2. "$" is used to identify an object (or understood as a variable). For example:
  9. For example, $ I, $ msg, $ TagUtil. options (...), etc.
  10. 3. "{}" is used to clearly identify the Velocity variable;
  11. For example, there is a $ someonename in the page. In this case, Velocity uses someonename as the variable name. If our program wants to display the name character after the someone variable, then the label above should be changed to $ {someone} name.
  12. 4 ,"! "Is used to forcibly display non-existent variables as blank.
  13. For example, if the page contains $ msg, the value of msg is displayed if the msg object has a value. If the msg object does not exist, the $ msg character is displayed on the page. This is not expected. In order to display a non-existent variable or an object whose value is null as a blank space, you only need to add a "!" before the variable name. .
  14. Example: $! Msg
  15. Five Basic Template script statements are provided to meet the requirements of all application templates. These four template statements are simple and can be directly added by the interface designer. In many EasyJWeb application practices, we can see that only the following four simple template script statements can be summarized in all interface templates:
  16. 1. $! Obj returns the object result directly.
  17. For example, the java object msg value is displayed in the html Tag. <P> $! Msg </p>
  18. Display the value of the msg object processed by the HtmlUtil object in the html tag <p >$! HtmlUtil. doSomething ($! Msg) </p>
  19. 2. # if ($! Obj) # else # end judgment statement
  20. For example, in EasyJWeb open-source applications, we often see examples for pop-up message msg.
  21. # If ($ msg)
  22. <Script>
  23. Alert ('$! Msg ');
  24. </Script>
  25. # End
  26. The above script indicates that when the object msg object exists, the content after <script> is output.
  27. 3. # foreach ($ info in $ list) $ info. someList # end cyclically reads the objects in the list of sets and processes them accordingly.
  28. For example, on the homepage of EasyJF open-source forum system (0.3), html Interface Template scripts with hot topics are displayed:
  29. # Foreach ($ info in $ hotList1)
  30. <A href = "/bbsdoc. ejf? EasyJWebCommand = show & cid = $! Info. cid "target =" _ blank ">$! Info. title </a> <br>
  31. # End
  32. The preceding script traverses objects in the hotList1 set cyclically and outputs related content of objects.
  33. 4. # macro (macroName) # end Script Function (macro) calls. It is not recommended to use them in a large number in the Interface Template.
  34. For example, in the example of adding, deleting, modifying, and querying quickly generated by EasyJWeb Tools, you can click the title bar of the List to display the results in ascending/descending order, this is a template content that we often see in EasyJWeb.
  35. Function (macro) definition, usually placed at the beginning
  36. # Macro (orderPic $ type)
  37. # If ($ orderField. equals ($ type ))
  38. # End
  39. # End
  40. For example, <font color = "# FFFFFF"> title # orderPic ("title") </font>
  41. 5. Include files # inclue ("template file name") or # parse ("template file name ")
  42. It is mainly used to process pages with the same content, such as the top or tail content of each website.
  43. For more information, see EasyJF open-source Blog and EasyJF open-source forum!
  44. For example: # parse ("/blog/top.html") or # include ("/blog/top.html ")
  45. The difference between parse and include is that if the included file contains the Velocity script tag, it will be further parsed, and include will be displayed as is.
  46. # Use of set
  47. Do not declare the Velocity script variable on the page view, that is, use # set as little as possible. Sometimes we need to display the serial number in the page, and the program object does not contain this serial number attribute, you can define it yourself. As shown in a circular system:
  48. # Set ($ I = 0)
  49. # Foreach ($ info in $ list)
  50. No.: $ I
  51. # Set ($ I = $ I + 1)
  52. # End
  53. Summary of Velocity script syntax
  54. 1. Declaration: # set ($ var = XXX)
  55. The following content can be left
  56. Variable reference
  57. String literal
  58. Property reference
  59. Method reference
  60. Number literal # set ($ I = 1)
  61. ArrayList # set ($ arr = ["yt1", "t2"])
  62. Arithmetic Operators
  63. 2. Notes:
  64. Single line ## XXX
  65. Multiple lines # * xxx
  66. Xxxx
  67. Xxxxxxxxxxxx *#
  68. References reference type
  69. 3. Variable Variables
  70. It must start with "$" and the first character must be a letter. Character followed by a VTL Identifier. (a. z or A. Z ).
  71. A variable can contain the following characters:
  72. Alphabetic (a. z, A. Z)
  73. Numeric (0 .. 9)
  74. Hyphen ("-")
  75. Underscore ("_")
  76. 4. Properties
  77. $ Identifier. Identifier
  78. $ User. name
  79. Name value in hashtable user. For example: user. get ("name ")
  80. 5. Methods
  81. Object user. getName () = $ user. getName ()
  82. 6. Formal Reference Notation
  83. Use {} to separate variable names from strings
  84. For example
  85. # Set ($ user = "csy "}
  86. $ {User} name
  87. Return csyname
  88. $ Username
  89. $! Username
  90. $ And $! Difference
  91. When the username cannot be found, $ username returns the string "$ username", while $! Username returns an empty string ""
  92. 7. Double quotation marks and quotation marks
  93. # Set ($ var = "helo ")
  94. Test "$ var" returns testhello
  95. Test' $ var 'returns test' $ var'
  96. You can change the default processing mode by setting stringliterals. interpolate = false.
  97. 8. Condition statements
  98. # If ($ foo)
  99. <Strong> Velocity! </Strong>
  100. # End
  101. # If ($ foo)
  102. # Elseif ()
  103. # Else
  104. # End
  105. When $ foo is null or a Boolean object's false value, it is executed.
  106. 9. logical operators: ==&||!
  107. 10. Loop statement # foreach ($ var in $ arrays) // The set contains the following three vectors: a Hashtable or an Array
  108. # End
  109. # Foreach ($ product in $ allProducts)
  110. <Li> $ product </li>
  111. # End
  112. # Foreach ($ key in $ allProducts. keySet ())
  113. <Li> Key: $ key-> Value: $ allProducts. get ($ key) </li>
  114. # End
  115. # Foreach ($ customer in $ customerList)
  116. <Tr> <td> $ velocityCount </td> <td> $ customer. Name </td> </tr>
  117. # End
  118. 11. The velocityCount variable is defined in the configuration file.
  119. # Default name of the loop counter
  120. # Variable reference.
  121. Directive. foreach. counter. name = velocityCount
  122. # Default starting value of the loop
  123. # Counter variable reference.
  124. Directive. foreach. counter. initial. value = 1
  125. 12. Include files
  126. # Include ("one.gif", "two.txt", "three.htm ")
  127. 13. Parse import script
  128. # Parse ("me. vm ")
  129. 14. # stop execution and return
  130. 15. Define the macro Velocimacros, which is equivalent to the function supporting the inclusion Function
  131. # Macro (d)
  132. <Tr> <td> </tr>
  133. # End
  134. Call
  135. # D ()
  136. 16. macros with Parameters
  137. # Macro (tablerows $ color $ somelist)
  138. # Foreach ($ something in $ somelist)
  139. <Tr> <td bgcolor = $ color> $ something </td> </tr>
  140. # End
  141. # End
  142. 17. Range Operator
  143. # Foreach ($ foo in [1 .. 5])

What is velocity?

A J2EE front-end template technology.
Similar to JSP and Freemarker, Freemarker is used to display webpage content.

Unlike JSP, velocity only displays data in Action and cannot process data. You cannot write java code, but you can use the Velocity mark.

The Velocity page (Template) is a file of any type (text/html.
For example, Action has the following two attributes.
Class XxxAction {
Private String title;
Private String name;

Public String execute (){
This. name = "Tom ";

This. title = "HelloWord ";

Return "velocity ";
}
// Getter & setter must provide getter
}

Struts. xml
<Result name = "velocity" type = "velocity"> a.html </result>

Velocity replaces the marked part.
A.html
<Html>
<Head>
<Title >$ {title} </title>

</Head>
<Body>
Hello $ {name}

</Body>
</Html>

The following page will be returned:

<Html>
<Head>
<Title> HelloWord </title>

</Head>
<Body>
Hello Tom

</Body>
</Html>
Note: In this example, you must add the velocity jar package under struts2.

What is velocity?

A J2EE front-end template technology.
Similar to JSP and Freemarker, Freemarker is used to display webpage content.

Unlike JSP, velocity only displays data in Action and cannot process data. You cannot write java code, but you can use the Velocity mark.

The Velocity page (Template) is a file of any type (text/html.
For example, Action has the following two attributes.
Class XxxAction {
Private String title;
Private String name;

Public String execute (){
This. name = "Tom ";

This. title = "HelloWord ";

Return "velocity ";
}
// Getter & setter must provide getter
}

Struts. xml
<Result name = "velocity" type = "velocity"> a.html </result>

Velocity replaces the marked part.
A.html
<Html>
<Head>
<Title >$ {title} </title>

</Head>
<Body>
Hello $ {name}

</Body>
</Html>

The following page will be returned:

<Html>
<Head>
<Title> HelloWord </title>

</Head>
<Body>
Hello Tom

</Body>
</Html>
Note: In this example, you must add the velocity jar package under struts2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.