关于模板语言Velocity的简单例子
2012-08-19 17:44
477 查看
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloVelocity {
public static void main(String[] args) throws Exception {
// 初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
// 取得velocity的模版,注意:test.vm放到你的项目根目录,不会src目录
Template t = ve.getTemplate("test.vm","UTF-8");
// 取得velocity的上下文context
VelocityContext context = new VelocityContext();
// 把数据填入上下文
context.put("name", "javaboy2012");
context.put("date", (new Date()).toString());
//将java类实例,填入上下文,在模板中使用
Cal c=new Cal();
context.put("caltool", c);
News news1=new News();
news1.setAuthor("author1");
news1.setId(1);
news1.setTitle("title1");
News news2=new News();
news2.setAuthor("author2");
news2.setId(2);
news2.setTitle("title2");
News news3=new News();
news3.setAuthor("author3");
news3.setId(3);
news3.setTitle("title3");
List<News> items = new java.util.ArrayList<News>();
items.add(news1);
items.add(news2);
items.add(news3);
//把list数据放入上下文,在模板中使用
context.put("items", items);
List temp = new ArrayList();
temp.add("1");
temp.add("2");
context.put("list", temp);
// 输出流
StringWriter writer = new StringWriter();
// 转换输出
t.merge(context, writer);
System.out.println(writer.toString());
}
}
public class Cal {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static int add(int a,int b)
{
return a+b;
}
}
public class News {
private int id;
private String title;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
<html>
<title>Hello Velocity</title>
<body>
Welcome $name to Javayou.com!
today is $date.
#foreach( $product in $list )
<li>$product</li>
#end
$caltool.add(1,2)
#foreach ($one in $items)
<li> 第 $velocityCount 个 <a href="${one.getId()}" title="${one.getTitle()}">${one.getAuthor()}</a></li>
#end
</body>
</html>
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloVelocity {
public static void main(String[] args) throws Exception {
// 初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
// 取得velocity的模版,注意:test.vm放到你的项目根目录,不会src目录
Template t = ve.getTemplate("test.vm","UTF-8");
// 取得velocity的上下文context
VelocityContext context = new VelocityContext();
// 把数据填入上下文
context.put("name", "javaboy2012");
context.put("date", (new Date()).toString());
//将java类实例,填入上下文,在模板中使用
Cal c=new Cal();
context.put("caltool", c);
News news1=new News();
news1.setAuthor("author1");
news1.setId(1);
news1.setTitle("title1");
News news2=new News();
news2.setAuthor("author2");
news2.setId(2);
news2.setTitle("title2");
News news3=new News();
news3.setAuthor("author3");
news3.setId(3);
news3.setTitle("title3");
List<News> items = new java.util.ArrayList<News>();
items.add(news1);
items.add(news2);
items.add(news3);
//把list数据放入上下文,在模板中使用
context.put("items", items);
List temp = new ArrayList();
temp.add("1");
temp.add("2");
context.put("list", temp);
// 输出流
StringWriter writer = new StringWriter();
// 转换输出
t.merge(context, writer);
System.out.println(writer.toString());
}
}
public class Cal {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static int add(int a,int b)
{
return a+b;
}
}
public class News {
private int id;
private String title;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
<html>
<title>Hello Velocity</title>
<body>
Welcome $name to Javayou.com!
today is $date.
#foreach( $product in $list )
<li>$product</li>
#end
$caltool.add(1,2)
#foreach ($one in $items)
<li> 第 $velocityCount 个 <a href="${one.getId()}" title="${one.getTitle()}">${one.getAuthor()}</a></li>
#end
</body>
</html>
相关文章推荐
- 模板语言velocity项目例子
- 关于Python的web模板jinja2的简单例子
- velocity模板引擎的基本语法(个人实际编写的例子)
- Java语言介绍(04)开源项目(04)模板引擎(01)Velocity
- 关于Repository、Autofac、DbContext简单例子
- 关于EJB3的一个简单例子
- DirectX 9高层着色语言介绍1 —— 引言、简单例子、汇编语言和编译对象
- Velocity模板语言(VM)介绍
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 读取文件
- 一种基于java语言的模板引擎velocity的使用
- c#关于Delegate最简单的例子:两个窗体间的消息传递
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第4章(估计要写9章)
- 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
- 一个简单的模板例子
- Go语言中转换JSON数据简单例子
- 模板语言:Velocity简介
- 字符串作为freemarker模板的简单实现例子
- [开源推荐]简单强大的模板语言JSON Template
- velocity模板语言之变量的赋值与使用区别小论
- 关于IBatisNet的两个简单CodeSmith模板