Based on Java-based website development, many people use JSP as the technology of the front-end web page, especially in China. This technology usually has some problems. I tried how we developed the website, usually there are several ways 1: After the function is determined, the UI (interface) section of the artistic design web page is then added to the code display. Logic (such as loop, determining display data results). That is, the usual JSP page production, of course, this part can be completed by the artist, then follow the JSP engineer to continue to create a corresponding JSP page as prototype. 2: After the function is determined, the UI (interface) section of the art design web page is added, and then the web crew will join the code display logic (such as loop, determining the display data result), in this step JSP page production, web page production Personnel (usually only JavaScript and HTML) learn how to embed JSP Taglib tags under engineers, and then make JSP web pages as the original template.
Obviously, one way is to be divided into more than one way, and then in many small companies, or the project is very anxious. JSP page production and background program development are the same person. This undoubtedly increased the burden of programmers. Although the latter case is better than the previous, it has two shortcomings: 1: Web producers must learn how to use JSP Taglib, which undoubtedly increases the burden of web crew. 2: If the page is designed from the new design because of the customer's request, no matter where the web crew is from the newly embedded JSP web page from the new display logic.
In this regard, JSP is not good, although from performance angle and Taglib's use, it is much stronger than PHP and ASP, but it is designed like PHP this server page language, which is embedded in the page. Scripting language technology, although it is fast than the traditional CGI-based script language, it is confused with the background program logic and the page, so from this perspective, it is a less design. Think about it, how do you see how many PHP programs look like, in a pile. In a PHP file, you have been unclear that those processes are only used to display the page.
Now more websites use a MVC mode, which is divided into work, which is M (Model, Model), V (View View), C (Controller Controller).
M (Model, model) is also the business logic of the background, the code, business logic, and more, etc. true for the transaction. They are the most important work part of the entire website, usually this part of the code is relatively stable, not often changed, there is no impact on the pages of the front end.
V (view view): This is also a web page display section, this part accepts the results or data from the background program, which is displayed, but this part is usually a big change, such as the website interface update is often necessary. Every time you update your webpage, you will cause a lot of changes to the View section.
C (Controller Controller). Transfer control between view and model, and call the data returned by the corresponding view display model, which is mainly responsible for dispatching.
What is the benefits of this responsibility? It simplifies the work of all relevant personnel in the process of software development, making different parts of the modification of different parts usually not affect the work of other parts, such as I have modified some programs in the background. The algorithm does not affect the page display of the front desk, and the front desk changes do not affect the development of the background program. This division of labor is much better than the JSP confusing code logic and the display layer. Therefore, more and more foreign programmers are constantly proposing an alternative JSP program, in many scenarios, a technique based on Java template engine, most famous is Velocity and Webmacro template technology.
The design idea of the template engine is the earliest of Webmacro, and later applied to a famous search engine www.altavista.com, this idea is gradually being adopted by the Apache Development Team, and as a subproject, this is now Velocity. The relationship between the template engine and the view in the MVC is more close. It is often used as a JSP alternative technology to appear in some forums abroad. But Velocity can be applied in any Java program that needs to format data.
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 because of the following reasons:
1: It is easy to integrate in a variety of programs. 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, makes 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 technological work that is replacing the JSP to do output pages in J2EE (Java 2 Platform, Enterprise Edition). Although JSP is included in J2EE specification, J2EE itself does not require JSP.
How does 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, it is easy to change into an HTML page)
Hello $ Name! Welcome to $ Site World!
The second is part of the Java program:
Below is the 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 * /
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 ());}}
Place the two files in the same directory, compile and run, 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, putting data into the environment / * 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 it is also very important, but it is very the same for each application:
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, Writer);
/ * Show the world * /
System.out.println (Writer.toString ());
Remember, this will be different in future servlet applications, because web output is not the same as the command line output, and 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 number of variable names. 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.
My website: http://www.eiffelqiu.comemail: Eiffelqiu@163.com hopes to communicate with you