[Java] template engine Velocity essay

Source: Internet
Author: User

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

Related Article

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.