⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 velocityengineandcontext.java

📁 《基于Eclipse的开源框架技术与实战》[第11章]随书源码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -