1. It can be argued that, in our simple view, its XSLT format is more complicated than those we have so far, and it is more difficult to understand. Although the format table is simple in the case of the XSLT. The view technology that proves to use XSLT as an example application is exemplified by the business needs of the example application. It will be a difficult thing.
2.xslt and XPath are preferably used when data has been used in XML form, but as shown in the examples of this section, the Java component model is converted to XML is also quite easy.
3.
So what is VELOCITY? Its official explanation is: "Velocity is a Java-based template engine that allows anyone to use simple and powerful template language to reference objects defined in Java code"
You may use Velocity: 1: it is easy to integrate in a wide variety of program fields because of the following reasons. 2: It provides a clear and simple syntax for webpage 3: Since the templates and code are separated, you can develop and maintain them separately. 4: The Velocity engine can be easily integrated into some Java runtime, especially servlet. 5: Velocity allows templates to access common methods in any environment object.
Velocity's power is its strict distinction between the program development function. It limits the object that the template may access (that is, the background program allows it to get the object) to achieve this. This means that web designers can only put their energy in the display section of the data (View view), and the programmer will pay attention to how to write a control layer of the program (Controller, controller) and business logic and data management (Model Model) This is the MVC development mode. MVC is now a widely accepted development model that simplifies development and increasingly complex applications and maintenance.
What are the best works in Velocity? 1: SERVET-based website production 2: Java and SQL code generation 3: XML processing and conversion 4: text processing, such as generating a TRF file.
However, Velocity uses the most of the Java Servlet, based on the Java Servlet webpage, to generate a web page, to replace JSP and other technologies. In addition to being easier to use, it provides a powerful template language to display and manipulate data, but it is important because this work should be part of the program logic. Velocity is ideal for the technical work of the J2EE (Java 2 Platform, Enterprise Edition), which acts as an alternative JSP to do output pages. Although JSP is included in J2EE specification, J2EE itself does not need JSP. How is Velocity work? ? Although most Velocity applications are servlet-based web pages. But in order to illustrate the use of Velocity, I decided to use the more common Java Application to clear its working principle.
It seems that all the language teaching is an example of the first program using helloWorld as the first program. It is no exception here.
Any Velocity app includes two aspects: The first is: template production, in our example is hellosite.vm: Its content is as follows (although it is not HTML, but this is easy to change into an HTML page)
Hello $ Name! Welcome to $ Site World!
The second is the Java program part: Here is Java 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 = new velocityContext (); context.put ("name", "eiffel qiu"); context.put ("Site", "http: //www.eiffelqiu. COM "); / * Now render the template Into a stringwriter * / stringwriter writewriter = new stringwriter (); t.mrge (context, write); / * show the world * / system.out.println (Writer.toString () );}} Putting two files in the same directory, compiling and running, the result is:
Hello Eiffel Qiu! Welcome to http://www.eiffelqiu.com world
In order to ensure the smooth operation, please download the Velocity's Run Pack from Velocity's website http://jakarta.apache.org/velocity/, put the path of the Velocity Jar package in the ClassPath of the system, so you can compile and run The above procedures are.
This program is simple, but it makes you clearly understand the basic working principle of Velocity. Other parts of the program are basically fixed, the main part is in the following code
Here Velocity gets template files to get template references
/ * Next, get the template * / template t = ve.getTemplate ("Hellosite.VM");
Here, initializing the environment and putting data into the environment
/ * CREATE A Context And Add Data * / VelocityContext (); Context.Put ("Name", "Eiffel Qiu"); Context.Put ("Site", "http://www.eiffelqiu.com ");
Other code is fixed, but it is also very important, but for each application is very the same: this is initializing the Velocity Template Engine
/ * first, get and initialize an engine * / velocityEngine VE = new velocityEngine (); ve.init ();
This is used to combine environment variables and output parts.
StringWriter Writer = new stringWriter (); t.merge (context, write); / * show the world * / system.out.println (Writer.tostring ()); remember, this will be in the future servlet application Difference, because the web output is not the same as the command line output, if used for web output, it will not output via System.out. This will explain to everyone in the later tutorial.
That let me sum up the true working principle of Velocity: Velocity solves how to pass data between servlet and web pages, of course, this mechanism for transmitting data is in the MVC mode, which is VIEW and MODLE, CONTROLLER Independently work independently, the one's modification does not affect other changes, and they are implemented between environmental variables, of course, both parties page make one party and the background program parties to each other to set a naming convention for the transfer variable, such as The Site, Name variables in the previous program, where they are on the web page is $ name, $ site. This will only be able to work independently of the two parties. Regardless of how the page changes, as long as the variable name is constant, then the rear program does not need to be changed, the front desk can also be modified by the web crew. This is the working principle of Velocity.
You will find that the simple variable name usually does not meet the needs of web pages to display data. For example, we often display some data sets, or determine how to display the next data according to some data, Velocity also provides loop, judging Simple syntax to meet the needs of web pages. Velocity provides a simple template language for use in front-end webmaster, this template language is simple enough (most people who know JavaScript can be mastered very quickly, in fact it is more simple than JavaScript), of course, this simple is deliberate Because it doesn't need anything, the View layer should not contain more logic. Velocity's simple template syntax can meet all your page display logic, this is usually enough, here will not occur here. JSP destroys the system in the system, JSP can do a lot of things, Sun is in the development of JSP 1.0 standard, there is no timely limit programmer in JSP insertion code logic, making early JSP code more php Code, it is powerful, but does not have to be confused, and the logical structure of the MVC three layers is confusing.