velocity模板引擎的基本语法(个人实际编写的例子)

本文介绍了使用Velocity模板引擎将VM文件转化为静态HTML文件的过程,包括常见代码示例及应用场景,涉及变量操作、条件判断、循环遍历、宏定义等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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代码,本人刚开始接触,望大家多多指点,一起学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值