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.