package com.javayou.velocity;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
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;
/**
* @author matol 2010-11-30
*/
public class HelloVelocity {
public static void main(String[] args) throws Exception {
try {
//初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
//取得velocity的模版
Template t = ve.getTemplate("/info.vm");
//取得velocity的上下文context
VelocityContext context = new VelocityContext();
//把数据填入上下文
context.put("info", "中石油吐哈油田");
context.put("cont", "冯培喜");
context.put("phone", "13152870917");
//context.put("date", (new Date()).toString());
//为后面的展示,提前输入List数值
List temp = new ArrayList();
for(int i=0;i< 10;i++){
temp.add("测试数据"+i);
}
context.put("list", temp);
//输出流
StringWriter writer = new StringWriter();
//转换输出
t.merge(context, writer);
//System.out.println(writer.toString());
/**//*查找文件,如果不存在,就创建*/
File file = new File("e:\\demo.doc");
FileWriter fw = new FileWriter(file);
if(!file.exists())
if(!file.createNewFile())throw new Exception("文件不存在,创建失败!");
fw.write(writer.toString());
fw.close();
} catch (Exception e) {
System.out.println("Error ..."+e.getMessage());
}finally{
System.out.println("Success ...");
}
}
public static void RunVelocity(){
try {
//初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
//取得velocity的模版
Template t = ve.getTemplate("E:/Java Project/VelocityDemo/WebRoot/vm/info.vm","GBK");
//取得velocity的上下文context
VelocityContext context = new VelocityContext();
//把数据填入上下文
context.put("info", "中石油吐哈油田");
context.put("cont", "冯培喜");
context.put("phone", "13152870917");
//context.put("date", (new Date()).toString());
//为后面的展示,提前输入List数值
List temp = new ArrayList();
for(int i=0;i< 10;i++){
temp.add("测试数据"+i);
}
context.put("list", temp);
//输出流
StringWriter writer = new StringWriter();
//转换输出
t.merge(context, writer);
//System.out.println(writer.toString());
writer.flush();
writer.close();
/**//*查找文件,如果不存在,就创建*/
File file = new File("e:\\demo.doc");
FileWriter fw = new FileWriter(file);
if(!file.exists())
if(!file.createNewFile())throw new Exception("文件不存在,创建失败!");
fw.write(writer.toString());
fw.close();
} catch (Exception e) {
System.out.println("Error ..."+e.getMessage());
}finally{
System.out.println("Success ...");
}
}
/*
* path是相对于WebRoot而言,的路径
*/
public static void exportFile(String path){
try {
Properties pro = new Properties();
pro.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
pro.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
pro.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
VelocityEngine ve=new VelocityEngine(pro);
Template t = ve.getTemplate("info.vm","utf-8");
VelocityContext context = new VelocityContext();
context.put("info", "中石油吐哈油田");
context.put("cont", "冯培喜");
context.put("phone", "13152870917");
List temp = new ArrayList();
for(int i=0;i< 10;i++){
temp.add("测试数据"+i);
}
context.put("list", temp);
PrintWriter writer = new PrintWriter("E:\\demo.doc","UTF-8");
t.merge(context, writer);
writer.flush();
writer.close();
} catch (Exception e) {
System.out.println("Error ..."+e.getMessage());
}
System.out.println("Success ...");
}
}
- 1
- 2
- 3
- 4
前往页