采用字符串作为模板内容的 Velocity简单实例。
2012-08-20 10:09
246 查看
package com.test.velocity;
import java.io.StringWriter;
import java.util.Date;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloVelocity {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// 初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
// 取得velocity的模版内容, 模板内容来自字符传
String content = "";
content += "Welcome $name to Javayou.com! ";
content += " today is $date.";
// 取得velocity的上下文context
VelocityContext context = new VelocityContext();
// 把数据填入上下文
context.put("name", "javaboy2012");
context.put("date", (new Date()).toString());
// 输出流
StringWriter writer = new StringWriter();
// 转换输出
ve.evaluate(context, writer, "", content); // 关键方法
System.out.println(writer.toString());
}
}
import java.io.StringWriter;
import java.util.Date;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloVelocity {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// 初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
// 取得velocity的模版内容, 模板内容来自字符传
String content = "";
content += "Welcome $name to Javayou.com! ";
content += " today is $date.";
// 取得velocity的上下文context
VelocityContext context = new VelocityContext();
// 把数据填入上下文
context.put("name", "javaboy2012");
context.put("date", (new Date()).toString());
// 输出流
StringWriter writer = new StringWriter();
// 转换输出
ve.evaluate(context, writer, "", content); // 关键方法
System.out.println(writer.toString());
}
}
相关文章推荐
- 字符串作为freemarker模板的简单实现例子
- 字符串作为freemarker模板的简单实现例子
- java 字符串作为freemarker模板的简单实现例子
- 字符串作为freemarker模板的简单实现例子
- 字符串作为freemarker模板的简单实现例子
- 字符串作为freemarker模板的简单实现例子
- (原创)模板字符串(短信,邮件..) 与 类实例 匹配替换的简单解决方案
- es6模板字符串中标签模板作为参数时产生空元素的问题
- log4j配置实例,简单实用,我用来做模板的
- Velocity模板引擎的简单使用
- velocity模板的使用简单的语法
- 使用JDBC连接数据库 查询数据库中内容 简单实例
- Qt TCP协议 传输简单字符串实例
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- 采用freemarker模板引擎作为Java邮件模板
- C++中将字符串作为参数传递,应采用什么类型变量
- 实例教程四:采用Pull解析器解析和生成XML内容
- JS判断文本框内容改变事件的简单实例
- Velocity 1.7之简单实例
- smarty简单模板变量输出方法[原创]_php实例_脚本之家