首页 > 代码库 > Velocity原理

Velocity原理

1、准备

添加引用:velocity-1.7.jar,velocity-tools-2.0.jar,commons-beanutils-1.7.0.jar,commons-chain-1.1.jar,commons-codec-1.6.jar,commons-collections-3.1.jar,commons-dbcp-1.4.jar,commons-digester-1.8.jar,commons-discovery-0.2.jar,commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang-2.6.jar,commons-logging-1.1.3.jar,commons-net-3.0.1.jar,commons-pool-1.5.5.jar

 

2、创建展示文件 hellosite.vm

内容:Hello $name! Welcome to $site world!

 

3、添加处理文件

package nankang.test;import java.io.StringWriter;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.VelocityEngine;public class Main {    /**     * @param args     */    public static void main(String[] args) {        try {            VelocityEngine ve = new VelocityEngine();            ve.init();            Template t = ve.getTemplate("hellosite.vm");            VelocityContext context = new VelocityContext();            context.put("name", "aaaaa");            context.put("site", "htt://www.baidu1.com");            StringWriter writer = new StringWriter();            t.merge(context, writer);            System.out.println(writer.toString());            System.out.println("成功");        } catch (Exception e) {            System.out.println(String.format("失败,%s", e.getMessage()));        }    }}

 

Velocity原理