`

Velocity之初识

阅读更多
一 环境: XP3+myeclipse6.6+JDK1.6
二 所需要的jar包: 见附件图片

三 示例代码
1 在src中创建一个example.vm,其内容如:
##声明一个变量:this并且设置值为:Velocity
#set( $this = "咫尺天涯")
##使用设置的变量值
这里是$this的博客!
##for打印集合信息数据
#foreach( $name in $list )
姓名:$name!
#end
##声明一个布尔值
#set( $condition = true)
## if else分支判断
#if ($condition)
The condition is true!
#else
The condition is false!
#end
姓名:$name,年龄:$age,性别:$sex
#foreach( $p in $listMap )
项目:$p.name,价格:$p.price;
#end

2 示例代码
package velocity;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

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


public class TestVelOcity {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		// 准备Velocity工作环境
		VelocityEngine ve = getVelocityEngine();

		// 准备数据
		VelocityContext context = getVelocityContext();

		// 准备模板文件
		Template template = ve.getTemplate("example.vm");

		
		if (template != null){
			// 准备输出流
			/*BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
			//此处很重要,设置输出内容与输出流,直接在控制台输出内容
			template.merge(context, writer);
			//关闭流
			writer.flush();
			writer.close();*/
			//以下可分开使用,此处只能选择一个来使用
			//获取模板内容:
			StringWriter getwriter = new StringWriter();
			template.merge(context, getwriter);
			String templateContext = getwriter.toString();			
			System.out.println(templateContext);			
		}				

	}

	// DAO返回数据
	@SuppressWarnings("unchecked")
	public static ArrayList getNames() {
		ArrayList list = new ArrayList();
		list.add("刘少奇");
		list.add("李逵");
		list.add("王熙凤");
		list.add("李四光");
		return list;
	}
	// DAO返回数据
	@SuppressWarnings("unchecked")
	public static ArrayList getNamesMap() {	
		ArrayList list = new ArrayList();
	    Map map = new HashMap();	
	    map.put("name", "书包");
	    map.put("price", "$100.00");
	    list.add( map );	
	    map = new HashMap();
	    map.put("name", "唱片");
	    map.put("price", "$59.99");
	    list.add( map );	
	    map = new HashMap();
	    map.put("name", "小菜");
	    map.put("price", "$3.99");
	    list.add( map );
		return list;
	}
	
	// DAO返回数据
	@SuppressWarnings("unchecked")
	public static Map getUserInfo() {
		Map<String, String> para = new HashMap<String, String>();
		para.put("name", "杨小燕 ");
		para.put("age", "45");
		para.put("sex", "女");
		return para;
	}	

	@SuppressWarnings("unchecked")
	private static VelocityContext getVelocityContext() {
		VelocityContext context = new VelocityContext();
		context.put("list", getNames());
		context.put("listMap", getNamesMap());		
		Map<String, String> para = getUserInfo();
		if (para.size() > 0) {
			for (String key : para.keySet()) {
				context.put(key, para.get(key));
			}
		}
		return context;
	}

	@SuppressWarnings("static-access")
	private static VelocityEngine getVelocityEngine() {		
		VelocityEngine ve = new VelocityEngine();
		Properties properties = new Properties();
		String fileDir = Thread.currentThread().getContextClassLoader()
				.getResource("").getPath();
		//String fileDir = TestVelOcity.class.getResource("/").getPath();
		// 将当前路径设置到VelocityEngine 中		
		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, fileDir);
		properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
		properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");		
		ve.init(properties);
		return ve;
	}

}

四 链接学习:
使用Spring计时器和velocity模板定时生成静态html/jsp文件
http://gundumw100.iteye.com/blog/702521

  • 大小: 13.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics