Velocity, velocity. js
- 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.
- I. Basic syntax
- 1. "#" is the script statement used to identify Velocity, including # set, # if, # else, # end, # foreach, # end, # iinclude, # parse, # macro, etc;
- For example:
- # If ($ info. imgs)
- # Else
- # End
- 2. "$" is used to identify an object (or understood as a variable). For example:
- For example, $ I, $ msg, $ TagUtil. options (...), etc.
- 3. "{}" is used to clearly identify the Velocity variable;
- 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.
- 4 ,"! "Is used to forcibly display non-existent variables as blank.
- 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. .
- Example: $! Msg
- 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:
- 1. $! Obj returns the object result directly.
- For example, the java object msg value is displayed in the html Tag. <P> $! Msg </p>
- Display the value of the msg object processed by the HtmlUtil object in the html tag <p >$! HtmlUtil. doSomething ($! Msg) </p>
- 2. # if ($! Obj) # else # end judgment statement
- For example, in EasyJWeb open-source applications, we often see examples for pop-up message msg.
- # If ($ msg)
- <Script>
- Alert ('$! Msg ');
- </Script>
- # End
- The above script indicates that when the object msg object exists, the content after <script> is output.
- 3. # foreach ($ info in $ list) $ info. someList # end cyclically reads the objects in the list of sets and processes them accordingly.
- For example, on the homepage of EasyJF open-source forum system (0.3), html Interface Template scripts with hot topics are displayed:
- # Foreach ($ info in $ hotList1)
- <A href = "/bbsdoc. ejf? EasyJWebCommand = show & cid = $! Info. cid "target =" _ blank ">$! Info. title </a> <br>
- # End
- The preceding script traverses objects in the hotList1 set cyclically and outputs related content of objects.
- 4. # macro (macroName) # end Script Function (macro) calls. It is not recommended to use them in a large number in the Interface Template.
- 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.
- Function (macro) definition, usually placed at the beginning
- # Macro (orderPic $ type)
- # If ($ orderField. equals ($ type ))
- # End
- # End
- For example, <font color = "# FFFFFF"> title # orderPic ("title") </font>
- 5. Include files # inclue ("template file name") or # parse ("template file name ")
- It is mainly used to process pages with the same content, such as the top or tail content of each website.
- For more information, see EasyJF open-source Blog and EasyJF open-source forum!
- For example: # parse ("/blog/top.html") or # include ("/blog/top.html ")
- 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.
- # Use of set
- 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:
- # Set ($ I = 0)
- # Foreach ($ info in $ list)
- No.: $ I
- # Set ($ I = $ I + 1)
- # End
- Summary of Velocity script syntax
- 1. Declaration: # set ($ var = XXX)
- The following content can be left
- Variable reference
- String literal
- Property reference
- Method reference
- Number literal # set ($ I = 1)
- ArrayList # set ($ arr = ["yt1", "t2"])
- Arithmetic Operators
- 2. Notes:
- Single line ## XXX
- Multiple lines # * xxx
- Xxxx
- Xxxxxxxxxxxx *#
- References reference type
- 3. Variable Variables
- It must start with "$" and the first character must be a letter. Character followed by a VTL Identifier. (a. z or A. Z ).
- A variable can contain the following characters:
- Alphabetic (a. z, A. Z)
- Numeric (0 .. 9)
- Hyphen ("-")
- Underscore ("_")
- 4. Properties
- $ Identifier. Identifier
- $ User. name
- Name value in hashtable user. For example: user. get ("name ")
- 5. Methods
- Object user. getName () = $ user. getName ()
- 6. Formal Reference Notation
- Use {} to separate variable names from strings
- For example
- # Set ($ user = "csy "}
- $ {User} name
- Return csyname
- $ Username
- $! Username
- $ And $! Difference
- When the username cannot be found, $ username returns the string "$ username", while $! Username returns an empty string ""
- 7. Double quotation marks and quotation marks
- # Set ($ var = "helo ")
- Test "$ var" returns testhello
- Test' $ var 'returns test' $ var'
- You can change the default processing mode by setting stringliterals. interpolate = false.
- 8. Condition statements
- # If ($ foo)
- <Strong> Velocity! </Strong>
- # End
- # If ($ foo)
- # Elseif ()
- # Else
- # End
- When $ foo is null or a Boolean object's false value, it is executed.
- 9. logical operators: ==&||!
- 10. Loop statement # foreach ($ var in $ arrays) // The set contains the following three vectors: a Hashtable or an Array
- # End
- # Foreach ($ product in $ allProducts)
- <Li> $ product </li>
- # End
- # Foreach ($ key in $ allProducts. keySet ())
- <Li> Key: $ key-> Value: $ allProducts. get ($ key) </li>
- # End
- # Foreach ($ customer in $ customerList)
- <Tr> <td> $ velocityCount </td> <td> $ customer. Name </td> </tr>
- # End
- 11. The velocityCount variable is defined in the configuration file.
- # Default name of the loop counter
- # Variable reference.
- Directive. foreach. counter. name = velocityCount
- # Default starting value of the loop
- # Counter variable reference.
- Directive. foreach. counter. initial. value = 1
- 12. Include files
- # Include ("one.gif", "two.txt", "three.htm ")
- 13. Parse import script
- # Parse ("me. vm ")
- 14. # stop execution and return
- 15. Define the macro Velocimacros, which is equivalent to the function supporting the inclusion Function
- # Macro (d)
- <Tr> <td> </tr>
- # End
- Call
- # D ()
- 16. macros with Parameters
- # Macro (tablerows $ color $ somelist)
- # Foreach ($ something in $ somelist)
- <Tr> <td bgcolor = $ color> $ something </td> </tr>
- # End
- # End
- 17. Range Operator
- # 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.