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 am trying to develop websites, there are usually 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 progress than the previous way, and then in many small companies, or the project is very accepted, JSP page production and background program development is the same. 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 website production uses a MVC mode, which is divided into workers, respectively, with 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 to 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 has been proposed by Webmacro, and later applied in a famous search engine http://www.altavista.com/, this idea is gradually being adopted by the Apache development group, and is mentioned as a subproject Come out, 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: 1 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 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 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. 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 section: 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", "chen WEI bo "); 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 ()); }} Putting two files in the same directory, compiling and running, the result is: Hello Eiffel Qiu! Welcome to http://www.eiffelqiu.com/World To ensure smooth running, please from Velocity's website http: // Jakarta.apache.org/Velocity/ Upload Velocity's Run Pack, place the path of the Velocity JAR package in the system's classpath so that you can compile and run the above programs. This program is simple, but it makes you clearly understand the basic working principle of Velocity.
Other parts of the program are basically very fixed, the most important part is in the following code here Velocity get template files, get template reference / * 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 * / vetocityContext context = new velocityContext (); context.put ("name", "eiffel qiu"); context.put ("site", " http://www.eiffelqiu.com "); other code is 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, 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.