1、首先新建一个工程,并导入jar包:
2、新建一个hellovelocity.vm
其代码如下:
<html>
<title>Hello Velocity</title>
<body>
================================
read the valiable
Welcome $name to Javayou.com! <br/>
today is $date.
================================
valiable definition
#set($iAmValiable="Good !")
$iAmValiable
================================
how to use if else sentence
#set($admin="admin")
#set($user="user")
#if($admin==$user)
Welcome admin!
#else
Welcome user!
#end
================================
foreach usage
#foreach($product in $list)
<li>$product</li>
#end
================================
foreach hash
#foreach($key in $hashVariable.keySet())
<li>$key's value:$hashVariable.get($key)</li>
#end
================================
java bean
#foreach($everystudent in $student)
<li>$everystudent.getNo()</li>
<li>$everystudent.getAddress()</li>
#end
---------------------------------
#foreach($everystudent in $student)
<$velocityCount>Address:$everystudent.getAddress()
#end
=================================
understand?
#foreach($element in $list)
#foreach($element in $list)
$velocityCount*****
#end
+++++++++++++++++++
$velocityCount*****
#end
==================================
#set($name="wangl")
${name}name
$namename
$!namename
$!{name}name
==================================
return the result of name
$!name
==================================
#if($name == "wangl")
#set($flag="success")
$flag
#else
#set($flag="error")
$flag
#end
===================================
#foreach($one in $list)
<$velocityCount>$one
#end
====================================
#macro(orderPic $strtt $dd)
#if($str.equals($dd))
name
#else
namename1
#end
#end
#orderPic("dd","dddd")
====================================
how to use include and parse
#include("/WebRoot/test.txt")
#parse("/WebRoot/test.vm")
=====================================
</body>
</html>
3、新建一个test.vm
其代码如下:
================================
================================
how to use if else sentence
#set($admin="admin")
#set($user="user")
#if($admin==$user)
Welcome admin!
#else
Welcome user!
#end
================================
================================
4、新建一个test.txt文件:
其内容如下:
here is printing from test.txt
5、新建一个HelloVelocity.java类
其代码如下:
package com.fasttalk.velocity;
import java.io.StringWriter;
import java.util.*;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
/**
* 模板引擎的一个例子:将vm文件转化成静态的html文件
* @author dell
*
*/
public class HelloVelocity {
public static void main(String [] args) throws Exception {
//初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
//取得velocity的模版
Template t = ve.getTemplate("./WebRoot/hellovelocity.vm");
//Template t1 = ve.getTemplate("./WebRoot/test.vm");
//Template t2 = ve.getTemplate("./WebRoot/test.txt");
//取得velocity的上下文context
VelocityContext context = new VelocityContext();
//把数据填入上下文
context.put("name", "Liang");
context.put("date", (new Date()).toString());
//为后面的展示,提前输入List数值
List temp = new ArrayList();
temp.add("1");
temp.add("2");
String str = "dddd";
context.put("str", str);
context.put("list", temp);
//添加hash代码
Hashtable hashVariable = new Hashtable();
hashVariable.put("1", "what ");
hashVariable.put("2", "are ");
hashVariable.put("3", "you ");
hashVariable.put("4", "doing ");
hashVariable.put("5", "?");
context.put("hashVariable", hashVariable);
//===对javabean的演示============================================
List student = new ArrayList();
student.add(new Student("001","北京市"));
student.add(new Student("002","中华人民共和国"));
context.put("student",student);//这里就是来定义后台将向前台发送一些数据
//输出流
StringWriter writer = new StringWriter();
//转换输出
t.merge(context, writer);
//t1.merge(context, writer);//这句话是想test.vm页面输出东西
System.out.println(writer.toString());
}
public static void VMBuilder() {
// TODO Auto-generated method stub
}
}
6、运行HelloVelocity.java类,控制台将会打印输出静态的html代码
以上例子包括了常用的velocity代码,本人刚开始接触,望大家多多指点,一起学习