Velocity is a Java-based template engine.
This blog post demonstrates the Helloword and branching conditions of Velocity.
HELLOWORD.VM, template file.
Templatedemo.java, demonstrating the Velocity template engine.
App.java, application of the entrance
On Eclipse, based on MAVEN management tools, the post-run directory structure is as follows
The source code exists only under the./src/main directory. The target directory is the directory where MAVEN generates the output and can be used as a reference. The test directory is the testing code directory, which can be ignored here.
Grs:test grs$ pwd/users/grs/documents/java/mavendemo/testgrs:test grs$ Tree.├──pom.xml├──src│├──main││├──java │││└──tony│││└──test│││├──App.java│││└──Templatedemo.java││└──resources││└──HELLOWORD.VM│└──test│└──java│└──tony│└──test│└──apptest.java└──target ├──classes│├──helloword.vm│└──tony│└──test│├──app.class│└──te Mplatedemo.class└──test-classes└──tony└──test└──apptest.class
Specific code
HELLOWORD.VM, template file
Hello $name! Wwwwww---#if ($value = = 1) value is 1, name was $name #elsevalue is not 1, name is $name #end
Templatedemo, demo code. It is important to note that when you get the current directory, you point to the root path of the current project, so when you look for the template file Helloword.vm, the path is also started from the project root directory.
Packagetony.test;ImportJava.io.File;ImportJava.io.StringWriter;Importorg.apache.velocity.Template;ImportOrg.apache.velocity.VelocityContext;ImportOrg.apache.velocity.app.VelocityEngine; Public classTemplatedemo { Public Static voidMain () {File file=NewFile ("."); System.out.println (File.getabsolutepath ()); Try{velocityengine ve=NewVelocityengine (); Ve.init (); Template Template= Ve.gettemplate ("./src/main/resources/helloword.vm"); Velocitycontext Context=NewVelocitycontext (); Context.put ("Name", "TTTTT"); Context.put ("Value", "1"); StringWriter writer=NewStringWriter (); Template.merge (context, writer); System.out.println (Writer.tostring ()); } Catch(Exception e) {e.printstacktrace (); } }}
App App Portal
Package Tony.test;public class App {public static void Main (string[] args) { templatedemo.main (); }}
Directory structure under Eclipse for reference
Resources
Start up the Velocity Template Engine, Javaworld
Introduction to the standard Directory Layout
[Java] template engine Velocity essay