This is an entry-level velocity article. Although it is very simple, it can indeed explain the working principle of velocity and is worth reading.
Develop websites using the velocity template engine
How does velocity work? Although most velocity applications are made based on servlet web pages. However, to illustrate the use of velocity, I decided to use a more general Java application to illustrate its working principles.
It seems that the beginning of all language teaching is helloworld as an example of the first program. This is no exception.
Any velocity application includes two aspects:
First, create a template. In our example, hellosite. VM:
Its content is as follows (although not mainly HTML, it is easy to change to an HTML page)
Hello $ name! Welcome to $ site world!
The second part is the Java program section:
The following is the Java code program code import java. Io. stringwriter;
Import org. Apache. Velocity. App. velocityengine;
Import org. Apache. Velocity. template;
Import org. Apache. Velocity. velocitycontext;
Public class helloworld {
Public static void main (string [] ARGs) throws exception {
/* First, get and initialize an engine */
Velocityengine VE = new velocityengine ();
Ve. INIT ();
/* Next, get the template */
Template T = VE. gettemplate ("hellosite. VM ");
/* Create a context and add data */
Velocitycontext context = new velocitycontext ();
Context. Put ("name", "Eiffel Qiu ");
Context. Put ("Site", "http://www.eiffelqiu.com ");
/* Now render the Template into a stringwriter */
Stringwriter writer = new stringwriter ();
T. Merge (context, writer );
/* Show the world */
System. Out. println (writer. tostring ());
}
}
Put the two files in the same directory and compile and run the files. The result is:
Hello Eiffel Qiu! Welcome to http://www.eiffelqiu.com world
To ensure smooth operation, download the velocity running package from the velocity website http://jakarta.apache.org/velocity/ and place the path of the velocity jar package in the classpath of the system, in this way, you can compile and run the above programs.
This program is simple, but it gives you a clear understanding of the basic working principles of velocity. The other parts of the program are basically fixed. The most important part is the following code:
Here, velocity gets the template file and gets the template reference.
Program code/* Next, get the template */
Template T = VE. gettemplate ("hellosite. VM ");
Here, initialize the environment and put the data into the environment
Program code/* create a context and add data */
Velocitycontext context = new velocitycontext ();
Context. Put ("name", "Eiffel Qiu ");
Context. Put ("Site", "http://www.eiffelqiu.com ");
Other code is relatively fixed, but also very important, but the writing method for each application is very same:
This is to initialize the velocity template engine.
Program code/* first, get and initialize an engine */
Velocityengine VE = new velocityengine ();
Ve. INIT ();
This is used to combine environment variables with the output part.
Program code stringwriter writer = new stringwriter ();
T. Merge (context, writer );
/* Show the world */
System. Out. println (writer. tostring ());
Remember, this will be different in future servlet applications, because the webpage output is not the same as the command line output. If it is used for webpage output, it will not be output through system. Out. This will be explained in future tutorials.
Let me summarize the true working principles of velocity:
Velocity solves the problem of how to transmit data between Servlet and webpage. Of course, this data transmission mechanism is implemented in MVC mode, that is, view and Modle and controller work independently of each other, modifications made by one party do not affect changes made by other parties. They are implemented through context, of course, the Website Creator and the background program of both parties should agree on the naming conventions for the variables passed. For example, in the previous program example, the site and name variables are $ name on the webpage, $ site. In this way, as long as both parties agree on the variable name, both parties can work independently. No matter how the page changes, as long as the variable name remains unchanged, the background program does not need to be changed. The front-end webpage can also be modified by the webpage creator. This is how velocity works.
You will find that simple variable names cannot meet the needs of creating and displaying data on the webpage. For example, we often show some datasets cyclically, or decide how to display the next data based on the values of some data, velocity also provides loops and simple judgment syntaxes to meet the needs of creating web pages. Velocity provides a simple template language for front-end web page makers to use. This template language is simple enough (most people who know JavaScript can quickly master it, in fact, it is much simpler than JavaScript), of course, this is simple and deliberate, because it does not need to do anything, the view layer should not contain more logic, the simple template syntax of velocity can meet all your requirements for page display logic. This is usually enough. Here, the system will not be destroyed because of an infinite loop statement like JSP, JSP can do a lot of things. When sun sets the JSP 1.0 standard, it does not promptly limit programmers to insert Code logic in JSP, making the early JSP code more like PHP code. Although it is powerful, however, it is unnecessary for the display layer logic and obfuscated the logic structure of the MVC layer-3.