Velocity uses functions to parse and load templates under the Web-inf folder

Source: Internet
Author: User
Package com.velocity.test;

Import Java.io.StringWriter;
Import java.util.Properties;

Import Org.apache.velocity.VelocityContext;
Import Org.apache.velocity.app.VelocityEngine;

/**

* Load template file from file

*122_ADD.VM in the web-inf/vm/folder * @author Welcome
*
*/
public class Loaderfromfile {

public static void Main (string[] args) throws exception{
Initialize parameters
Properties Properties=new properties ();
Set the velocity resource load mode to class
Properties.setproperty ("Resource.loader", "file");
To set the processing class when the velocity resource is loaded as file
Properties.setproperty (Velocity.file_resource_loader_path,
This.getclass (). GetResource ("/"). ToString (). ReplaceAll ("^file:/", "" "). ReplaceAll (" Classes "," VM "));
Instantiate a Velocityengine object
Velocityengine velocityengine=new Velocityengine (properties);

Instantiate a Velocitycontext
Velocitycontext context=new Velocitycontext ();
Put a key value into the Velocitycontext
Context.put ("username", "Zhang San");
Context.put ("Password", "123456789");
Context.put ("Age", "20");
Context.put ("Address", "Xi ' an, Shaanxi");
Context.put ("blog", "http://blogjava.net/sxyx2008");
Instantiate a StringWriter
StringWriter writer=new StringWriter ();

Template Template =velocityengine.gettemplate ("122_ADD.VM");
Template.merge (context, writer);
System.out.println (writer);

Velocityengine.mergetemplate ("Hello.vm", "GBK", context, writer);
System.out.println (Writer.tostring ());
}
}

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.