velocityengineandcontext.java

来自「《基于Eclipse的开源框架技术与实战》[第11章]随书源码」· Java 代码 · 共 46 行

JAVA
46
字号
package com.free.vtl.engine;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

/**
 * <p>Title: Eclipse Plugin Development</p>
 * <p>Description: Free download</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Free</p>
 * @author gan.shu.man
 * @version 1.0
 */

public class VelocityEngineAndContext {

	private static final String TEMPLATE_NAME = "./WEB-INF/src/VelocityEngineAndContext.vm";

	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.put("input.encoding", "utf-8");
		// 创建模板引擎
		VelocityEngine ve = new VelocityEngine();
		// 初始化引擎
		ve.init(props);

		Template t = ve.getTemplate(TEMPLATE_NAME);
		//创建模板上下文
		VelocityContext context1 = new VelocityContext();
		VelocityContext context2 = new VelocityContext(context1);

		context1.put("firstName", "gan");
		context2.put("lastName", "shu.man");

		Writer writer = new StringWriter();
		t.merge(context2, writer);

		System.out.println(writer.toString());
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?