Apache velocity templated

Source: Internet
Author: User
ArticleDirectory
    • 2. advantages of using velocity:
    • 2.3.velocity is best at the following tasks:
    • 3. 1. instance 1
    • 3. 2. instance 2
1. References

Http://www.cnblogs.com/zhuboxingzbx/articles/1225103.html

Http://www.cnblogs.com/amboyna/archive/2008/06/19/1225105.html

2. Overview 2.1.velocity template engine

The template engine design concept 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.

The template engine is more closely related to the visual part in MVC. Velocity can be applied to any JavaProgram. So what is velocity? Its official explanation is: "velocity is a Java-based template engine that allows anyone to reference definitions in Java using a simple and powerful template language.Code."

2. advantages of using velocity:
    • It is easy to integrate in a variety of program fields;
    • Provides a clear and simple syntax for the Website Creator;
    • Because templates and codes are separated, they can be independently developed and maintained;
    • The velocity engine can be easily integrated into some Java runtime environments, especially servlet;
    • Velocity allows the template to access common methods in any environment object.

Velocity is also powerful in its strict division of responsibility for program development functions. This is achieved by limiting the objects that the template may access (that is, the objects that the background program allows it to obtain. This means that web designers can focus only on the display part of the data, while programmers only need to focus on how to write the control layer (Controller) of the program), business logic and Data Management (model ). This is a typical MVC development model, which simplifies development and increasingly complex application and maintenance work.
2.3.velocity is best at the following tasks:

    • ◆ Servlet-based website creation;
    • ◆ Java and SQL code generation;
    • ◆ XML processing and conversion;
    • ◆ Text processing, such as generating TRF files.

However, velocity is used most often in Java Servlet-based web page programs to generate Web pages, replacing JSP and other technologies. In addition to ease of use, it provides a powerful template language for displaying and operating data. Note that it is important not to generate data because the generation should be part of the program logic.

Velocity is very suitable for replacing JSP in the Development of J2EE (Java 2 platform, Enterprise Edition) websites for the technical work of output pages. Although JSP is included in J2EE specifications, J2EE itself does not need JSP.

3. instance 3. 1. instance 1

The template (hellosite. VM) only provides some static characters, with some classes such as $ title "wildcard" added in the middle, which should be replaced in helloworld. java. The code for hellosite. VM is as follows:

Hello $ name! Welcome to $ site world!

Helloworld. Java is the processing program, and the annotations are clearly written. The following is the code of helloworld. Java:

Import Java. io. stringwriter; <br/> Import Org. apache. velocity. app. velocityengine; <br/> Import Org. apache. velocity. template; <br/> Import Org. apache. velocity. velocitycontext; </P> <p> public class helloworld {<br/> Public static void main (string [] ARGs) throws exception {<br/>/* first, get and initialize an engine */<br/> velocityengine VE = new velocityengine (); <br/> ve. init (); <br/>/* Next, get the template */<br/> template T = ve. gettemplate ("hellosite. VM "); <br/>/* create a context and add data */<br/> velocitycontext context = new velocitycontext (); <br/> context. put ("name", "xuwei"); <br/> context. put ("Site", "http://blog.csdn.net/xw13106209"); <br/>/* now render the Template into a stringwriter */<br/> stringwriter writer = new stringwriter (); <br/> T. merge (context, writer); <br/>/* show the world */<br/> system. out. println (writer. tostring (); <br/>}< br/>}

3. 2. instance 2

The template here is the same as that of instance 1, and the Java code is not as good as the code below.

Import Java. io. stringwriter; <br/> Import Org. apache. velocity. app. velocityengine; <br/> Import Org. apache. velocity. template; <br/> Import Org. apache. velocity. velocitycontext; </P> <p> public class helloworld2 {<br/> Public static void main (string [] ARGs) throws exception {</P> <p> // initialize the velocity template engine <br/> velocityengine VE = new velocityengine (); <br/> ve. init ("d :\\ Documents \ workspace \ velocitytest \. properties "); </P> <p> // obtain the template file and obtain the template reference. <br/> template T = ve. gettemplate ("hellosite. VM "); </P> <p> // initialize the environment and put the data into the environment <br/> velocitycontext context = new velocitycontext (); <br/> context. put ("name", "xuwei"); <br/> context. put ("Site", "http://blog.csdn.net/xw13106209"); </P> <p> // combine environment variables with the output part <br/> stringwriter writer = new stringwriter (); <br/> T. merge (context, writer); <br/> system. out. println (writer. tostring (); <br/>}< br/>}Here, the main function of the configuration file is to provide the path of the. properties file. The following is the code of A. properties:
<textarea readonly name="code" class="java">Runtime. log = velocity_example.log <br/> input. encoding = GBK <br/> output. encoding = GBK <br/> file. resource. loader. path = D: \ Documents \ workspace \ velocitytest</textarea>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.