The design idea of the velocity Technology Survey template engine was first proposed by webmacro and applied to a famous search engine www.altavista.com. Later, this idea was gradually adopted by the Apache development team and proposed as a sub-project. This is the current velocity.
What is velocity?Velocity is a Java-based template engine ). It allows anyone to simply use the template language to reference objects defined by Java code. When velocity is applied to web development, the interface designer can develop a web site following the MVC Architecture in sync with Java program developers. That is to say, the page designer can only focus on the page display effect, java developers focus on business logic encoding. Velocity separates Java code from web pages, which facilitates the long-term maintenance of web sites and provides an optional solution besides JSP and PHP. Velocity is far more powerful than Web site development. For example, it can generate SQL, postscript, and XML from templates, it can also be used as an independent tool to generate source code and reports, or as an integrated component of other systems.
Velocity
What can we do for us?You may use velocity: 1 for the following reasons: it is easy to integrate into a variety of application fields. 2: it provides a clear and simple syntax for web page makers. 3. Because templates and code are separated, you can develop and maintain them independently. 4: The velocity engine can be easily integrated into some Java runtime environments, especially servlet. 5: velocity allows the template to access the common methods in any environment object. Velocity is powerful in its strict division of responsibility for program development functions. It limits the objects that the template may access (that is, the objects that the background program allows it to obtain) to achieve this. This means that web designers can focus only on the display part of the data (view), while programmers only need to focus on how to write the control layer (controller, Controller) of the program) and business logic and Data Management (model), which is the MVC development model. MVC is now a widely accepted development model that simplifies development and increasingly complex application and maintenance work.
For example
:
Suppose you are a page designer who specializes in selling mud online stores. Let's call it "online mud store" for the time being ". Your business is very prosperous. The customer has placed various types and quantities of mud orders. They log on to your website only after entering the user name and password. After logging on, they are allowed to view the order and purchase more muds. Now, a very popular mud is selling at a discount. In addition, some customers regularly purchase the other, which is also at a discount but not very popular, bright red mud. because there are not many buyers, it is placed on the edge of the page. All user information is tracked and stored in the database, so one day there may be a problem: why not use velocity to make users better browse the products they are interested in? Velocity makes it easy to customize web pages.
Velocity
Workflow and PrincipleVelocity is an open-source Java template engine. In short, the template engine is used to obtain and process data and display the data. For example, if there is a list of enterprise employees, we can use the template engine to display it on the enterprise's Intranet in the form of HTML pages, or simply display it on the Administrator's mobile phone. The advantage of using the template engine is that developers and designers can operate and use data in the most appropriate way. velocity is an excellent example of a template engine. The English meaning of velocity is "speed, speed, speed; fast". Maybe its authors hope that velocity can speed up the development of Web applications. When velocity is applied to an application program or a servlet, you usually do the following: 1. initialize velocity.2. create context object 3. add data to context4. select template 5. merge the template and your data to generate the output page incodeusingthe Singleton pattern via
org.apache.velocity.app.Velocity
Class, this looks like
Import java. Io. stringwriter;
Import org. Apache. Velocity. velocitycontext;
Import org. Apache. Velocity. template;
Import org. Apache. Velocity. App. velocity;
Import org. Apache. Velocity. Exception. resourcenotfoundexception;
Import org. Apache. Velocity. Exception. parseerrorexception;
Import org. Apache. Velocity. Exception. methodinvocationexception;
Velocity. INIT ();
Velocitycontext context = new velocitycontext ();
Context. Put ("name", new string ("velocity "));
Template template = NULL;
Try ...{
Template = velocity. gettemplate ("mytemplate. VM ");
}
Catch (resourcenotfoundexception rnfe)
... {// Couldn't find the template}
Catch (parseerrorexception pee)
... {// Syntax error: Problem parsing the template} catch (methodinvocationexception MIE)
... {// Something invoked in the template // threw an exception} catch (exception E)
...{}
Stringwriter Sw = new stringwriter ();
Template. Merge (context, SW );
Velocity
InternationalVelocity itself supports template internationalization. Let's take a look at the method provided by velocity: Public template gettemplate (stirng template, string encoding). In addition, in velocity's tools: messagetool, the variable text contains the international tag, so you only need to write the tag Code, for example, $ text. get ('title ').
Velocity
Pair
Framework
Support
1.
Velocity tools/velocityviewservlet
Pair
Servlet
Support
,
Use
Velocityviewservlet
To replace
S
Ervlet
.
2.
Velocity tools/velocitystruts
Pair
Struts
Support
.
3.
Third party frameworks
Third-party framework support
,
For example
:
Spring, turbine
And so on
.
Velocity
Benefits and
DisadvantagesIt fully embodies the core idea of MVC, that is, functional modularization. Splitting HTML technology and complex business logic. The advantage of velocity lies in its simple template syntax and the Successful separation of Content and Display methods. With velocity, developers can focus on program code. Designers can focus on page layout and visual effects without having to worry about embedding various program codes in templates. This is obviously a major improvement compared to the traditional method of constructing pages using JSP and PHP. In traditional JSP and PHP pages, program logic is embedded into web pages, which brings great trouble to understand and maintain web pages. The velocity template engine successfully solved this problem. The main disadvantage of velocity is Exception Handling and logs.
Summary 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, controllers work independently of each other. modifications made by one party do not affect changes made by others. 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.