Velocity template engine

Source: Internet
Author: User

Package Zoer;

Import java. Io. stringwriter;

Import org. Apache. Velocity. template;
Import org. Apache. Velocity. velocitycontext;
Import org. Apache. Velocity. App. velocityengine;

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", "Mason ");
Context. Put ("Site", "naughty's ");
/* Now render the Template into a stringwriter */
Stringwriter writer = new stringwriter ();
T. Merge (context, writer );
/* Show the world */
System. Out. println (writer. tostring ());
}
}

Create a hellosite. VM in the project root directory. content:

Hello $ name! Welcome to $ site world!

When the preceding Java code is executed, the response html code is generated. Here is a simple example. It is not a web project.

After a brief look at the velocity template engine, the advantage is that JSP is abandoned. Although JSP is a part of J2EE, J2EE does not necessarily use JSP. You can also use templates such as velocity, MVC can also be implemented to separate code logic from page display.

PS: After learning about the Django framework, let's look at the velocity template engine. They feel that their templates are similar to their usage. Are extremely simple.


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.