The simplest example of implementing templated Apache Velocity

Source: Internet
Author: User
News has not only been published so far, but I have also published news that can generate static pages before. However, I always feel that it is not very professional and inefficient.

Apache velocity has been found. It can be used in many fields. Currently, it is only used to display pages in struts to avoid overload of the same URL (such as shownews. do, maybe n people watch the news at the same time. The detailed content of this news shows that the URL load is too heavy ). Well, the general principle is as follows:

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

Hello $ name ! Welcome to $ site world !

Helloworld. Java is processingProgramThe annotations are clearly written. The following is the code of helloworld. Java:ImportJava. Io. stringwriter;
ImportOrg. Apache. Velocity. App. velocity;
ImportOrg. Apache. Velocity. App. velocityengine;
ImportOrg. Apache. Velocity. template;
ImportOrg. Apache. Velocity. velocitycontext;

Public   Class Helloworld {
Public   Static   Void Main (string [] ARGs) Throws Exception {

// Initialize the velocity template engine
Velocityengine ve =   New Velocityengine ();
Ve. INIT ( " D: \ working \ velocity \ AAAA \ bin \ A. Properties " );

// Velocity obtains the template file and obtains the template reference.
Template t = Ve. gettemplate ( " H. VM " );

// Initialize the environment and put the data into the environment
Velocitycontext Context =   New Velocitycontext ();
Context. Put ( " Name " , " Zz " );
Context. Put ( " Site " , " Http://amboyna.cnblogs.com " );

// Combine environment variables with output
Stringwriter writer =   New Stringwriter ();
T. Merge (context, writer );
System. Out. println (writer. tostring ());
}
}

Here, the main role of the configuration file is to provide the path, not to mention, the following is the code of A. properties:Runtime. Log=Velocity_example.log
Input. Encoding=GBK
Output. Encoding=GBK
File. Resource. loader. Path=D: \ working \ velocity \ AAAA \ bin

Finally, complete the acquisition ....

By the way, click to download the program code. Program code

For a complete introduction to Apache velocity, refer to this: Use the Apache velocity template engine to quickly create a website.

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.