关于模板语言Velocity的简单例子
来源:互联网 发布:linux服务器虚拟化方案 编辑:程序博客网 时间:2024/06/06 02:57
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>
- 关于模板语言Velocity的简单例子
- 模板语言velocity项目例子
- velocity模板的使用简单的语法
- Velocity模板引擎的简单使用
- Velocity模板引擎的简单使用
- Velocity模板引擎的简单使用
- velocity简单例子
- velocity简单例子
- 模板语言:Velocity简介
- 模板语言velocity
- 关于Python的web模板jinja2的简单例子
- 简单利用模板的例子
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- 用Delphi模拟键盘输入
- vc++获取当前使用IE的URL路径与页面的源代码
- 生成GUID字符串
- 2012系统架构设计师考试大纲
- C# GC.Collect()
- 关于模板语言Velocity的简单例子
- 我的面试,我的学习,我的经历
- HDU 2069 动态规划
- 腾讯微博 android Oauth2.0 SDK 使用介绍
- 写正确函数需要注意的地方:最大堆的创建与堆排序
- sqlserver 纯分页
- UNIX网络编程
- Android4.0 StatusBar状态栏启动
- android2.1状态栏显示耳机图标