[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>