Velocity study documents

Source: Internet
Author: User

1. Overview:
A) Velocity is a java-based template engine, VTL for short.
B) The main purpose is to make up for the shortcomings of JSP and free the page design from the heavy java coding.
C) use a novel syntax format, which is concise and efficient.
2. Basic Concepts
A) although the Velocity syntax is not completely similar to java, it is also based on object-oriented syntax specifications.
B) The basic language elements and control statements are shown in appendix.
I. '#' All Velocity statements start with it and do not need special symbols at the end;
For example:
# If ($ foo = $ bar)
# If ($ foo> 42)
# If ($ foo <42)
Ii. '$' all variables, attributes, and objects start to be used;
For example:
$ Mud-Slinger_9
$! Mud-Slinger_9
$ {Mud-Slinger_9}
Iii. variables;
1. Regular Expression:
$ [! ] [{} [A. z, A. Z] [a. z, A. Z, 0. 9,-, _] []
For example:
Normal notation: $ mud-Slinger_9
Silent notation: $! Mud-Slinger_9
Formal notation: $ {mud-Slinger_9}
2. There are three basic forms of variables:
Normal notation (Normal notation) is the most common variable type in Velocity and can be used in most cases;
Silent notation is mainly used when you use a variable, but it does not have a value, it will output an empty string for you;
Formal notation. This type is mainly used to differentiate variable names. For example, when the variable $ test is used, the content of the page is followed by the content of the page, for example: $ testHello, the variable is mistakenly identified as $ testHello, so the change is as follows: $ {test} Hello, which can be used normally;
3. Variable comments:
In Velocity, '$' is reserved, so the use of it on the page will be limited, the following page content:
$2003 year, there is no problem, because in Velocity, the variable must start with the mother ('! ',' {'Except}, so this string will be displayed normally;
$ Year2003: The following strings are displayed: 1. however, the variable name is not defined at present, and the unified sample will be displayed normally. In other words, if a variable is to be used but not defined, the system will print it as a general string; 2. if this variable name is defined, the system treats all the strings that appear as variables. To avoid this, Velocity provides a special symbol modifier '\', assume that the variable is assigned good year, and the result is as follows:
$ Year2003 good year
\ $ Year2003 $ year2003
\ $ Year2003 \ good year
\\\$ Year2003 \ $ year2003
And so on.
Note: using variables independently, such as <input type = "text" name = "email" value = "$ email"/> is not a complete statement, you do not need to add '#' at the beginning of the line '#'
Iv. Nature:
$ [{} [.. Z, .. z] [.. z, .. z, 0 .. 9,-, _] *. [.. z, .. z] [.. z, A-Z, 0 .. 9,-, _] * []
3. Example
A) An example of parameter settings,
<Html>
<Body> HELLO $ CUSTOMERNAME </body>
</Html>
We can get the customer's name at runtime and insert it into this template to replace the variable $ CUSTOMERNAME. The entire replacement process is controlled by Velocity, the java call code is also very simple, for example, we can call it in java code.
/*************************************** ********************/
// This file sets the log4j configuration used by Velocity and the directory where the template file of Velocity is located.
Velocity. init ("D: \ Template \ resource \ jt. properties ");
// Template file name. The path of the template file has been set in the previous statement.
Template template = Velocity. getTemplate ("hello. vm", "gb2312 ");
// Instantiate a Context
VelocityContext context = new VelocityContext ();
// Set the value of the template variable to context.
Context. put ("CUSTOMERNAME", "My First Template Engine ---- Velocity .");
// Start template replacement
Template. merge (context, writer );
// Write to file
PrintWriter filewriter = new PrintWriter (new FileOutputStream (outpath), true );
Filewriter. println (writer. toString ());
Filewriter. close ();
B) An example of an operation object
<Html>
<Body>
Name: $ Customer. Name ()
Address: $ Customer. Address ()
Age: $ Customer. Age ()
</Body>
</Html>

Java code:
/*************************************** ********************/
// Set customer information
Customer mycustomer = new Customer ();
Mycustomer. setName ("Velocity ");
Mycustomer. setAddress ("jakarta.apache.org/velocity/index.html ");
Mycustomer. setAge (2 );
// This file sets the Log4j configuration used by Velocity and the directory where the mode file of Velocity is located. init ("D :\\ Template \ resource \ jt. properties ");
// Template file name. The path of the template file has been set in the previous statement.
Template template = Velocity. getTemplate ("hello. vm", "gb2312 ");
// Instantiate a Context
VelocityContext context = new VelocityContext ();
// Set the value of the template variable to context.
Context. put ("Customer", mycustomer );
// Start template replacement
Template. merge (context, writer );
// Write to file
PrintWriter filewriter = new PrintWriter (new FileOutputStream (outpath), true );
Filewriter. println (writer. toString ());
Filewriter. close ();
Output result:
<Html>
<Body>
Name: Velocity
Address: jakarta.apache.org/velocity/index.html
Age: 2
</Body>
</Html>
4. Comparison with JSP
Velocity vs. JSP: Both templates that implement the same function. JSP is Sun standard, and Velocity is a system developed by Open Source. From the Open Source perspective, JSP is a "unavailable" product developed by Sun. "Not easy to use" is mainly reflected in: difficult to Debug; there is no need to complicate simple problems, such as JSP First compilation before running; cannot prevent users from embedding Java code into JSP, this damages the MVC design. in addition, Sun named JSP as "Standard", with the suspicion of "threatening tianzi to make princes. Of course, Struts and Tag Lib remove Java code from JSP, saving JSP.

 

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.