Velocity 学习实例

本文介绍了一种名为Velocity的视图技术,并提供了一个简单的示例项目。该项目展示了如何使用Velocity进行页面渲染,包括配置环境、创建JavaBean、编写Servlet处理逻辑及Velocity模板文件。

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

[color=darkred]因项目需要,最近学习了一种新的视图技术Velocity,就顺便写了一个小例子,希望对初学者有所帮助![/color]

1.以下是工程目录结构
[img]http://dl.iteye.com/upload/attachment/178347/70f0286f-6f93-36bb-b46a-ba17bf5b9b09.jpg[/img]

2.需要用到的包有:velocity-1.4.jar / velocity-dep-1.3.1.jar

3.Person.java 代码

package com.cn.test;

public class Person {
private String firstname;
private String lastname;
private String phone;

public String getFirstname() {
return firstname;
}

public String getLastname() {
return lastname;
}

public String getPhone() {
return phone;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public void setPhone(String phone) {
this.phone = phone;
}
}


4.PersonServlet.java 代码

package com.cn.test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.velocity.*;
import org.apache.velocity.context.*;
import org.apache.velocity.servlet.*;
import org.apache.velocity.app.*;
import org.apache.velocity.exception.*;

public class PersonServlet extends VelocityServlet {

protected Properties loadConfiguration(ServletConfig config)
throws IOException, FileNotFoundException {

/*
* 设置模板文件在web应用中的位置
*/
Properties p = new Properties();
String path = config.getServletContext().getRealPath("/");
if (path == null) {
System.out.println("resources not found ");
path = "/";
}
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
p.setProperty("runtime.log", path + "velocity.log");

return p;
}

public Template handleRequest(HttpServletRequest request,
HttpServletResponse response, Context context) {
// 创建模版对象
Template template = null;

try {
// 创建集合对象
Hashtable<String, Person> personlist = new Hashtable<String, Person>();

// 设置信息
Person person = new Person();
person.setFirstname("ZhouXiang");
person.setLastname("Fei");
person.setPhone("86249466");
personlist.put(person.getFirstname(), person);

person = new Person();
person.setFirstname("WenLi");
person.setLastname("Jun");
person.setPhone("22126536");
personlist.put(person.getFirstname(), person);

person = new Person();
person.setFirstname("KuRu");
person.setLastname("Two");
person.setPhone("45682546");
personlist.put(person.getFirstname(), person);

context.put("personlist", personlist); // 将模板数据 personlist 放置到上下文环境 context 中去

template = getTemplate("information.vm"); // 加载模板
} catch (ParseErrorException ex1) { // 可能出现的异常
System.out.println("VelocityAdd: parse error for template " + ex1);
} catch (ResourceNotFoundException ex2) {
System.out.println("VelocityAdd: template not found " + ex2);
} catch (Exception ex3) {
System.out.println("VelocityAdd: error " + ex3);
}
return template;
}

}



5. information.vm 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Velocity Example</title>
<meta http-equiv="keywords" content="enter,your,keywords,here" />
<meta http-equiv="description" content="A short description of this page." />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</head>
<body>
<h1>This is My First Velocity Example</h1>
<table border="1">
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Phone</td>
</tr>
#foreach($list in $personlist>
<tr>
<td>$list.firstname</td>
<td>$list.lastname</td>
<td>$list.phone</td>
</tr>
#end
</table>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值