示例部分
1.Hello world的示例代码:
(1)Velocity模板(hello.html)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
hello,$name! (注意:这里的name与VelocityTest.java中的名称要一致)
</BODY>
</HTML>
(2)将velocity模板的内容转换的类(VelocityTest.java)
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
/**
* Velocity转换
* @author
*/
public class VelocityTest
{
/**
* 主函数
* @param args
*/
public static void main(String[] args)
{
//获取模板引擎
VelocityEngine ve = new VelocityEngine();
//模板文件所在的路径
String path = "D:/java/jproject/regedit/webroot";
//设置参数
ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
//处理中文问题
ve.setProperty(Velocity.INPUT_ENCODING,"GBK");
ve.setProperty(Velocity.OUTPUT_ENCODING,"GBK");
try
{
//初始化模板
ve.init();
//获取模板(hello.html)
Velocity模板的名称 |
//获取上下文
VelocityContext root = new VelocityContext();
//把数据填入上下文
root.put("name","world"); (注意:与上面的对应)
//输出路径
Strint outpath = "e:/helloworld.html";
//输出
Writer mywriter = new PrintWriter(new FileOutputStream(
new File(outpath)));
template.merge(root, mywriter);
mywriter.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
(3)环境的搭建
在lib目录内分别copy进:velocity-1.4.jar,velocity-dept.jar;
下载地址:http://jakarta.apache.org/velocity/
(4)运行后的结果如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
hello,world!
</BODY>
</HTML>
2.Servlet和Velocity结合示例
(1)example.html
<html>
<head><title>Velocity</title></head>
<body bgcolor="#ffffff">
<center>
<h2>Welcom to Velocity!</h2>
<i>Here's the list of people</i>
<table cellspacing="0" cellpadding="5" width="20%" >
<tr>
<td bgcolor="#eeeeee" align="center">
Names:
</td>
</tr>
#foreach ($name in $theList)
<tr>
<td bgcolor="#eeeeee" align="center">$name</td>
</tr>
#end
</table>
</center>
</body>
</html>
(2)servlet
package com.koal.velocity;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
public class SampleServlet extends VelocityServlet
{
/**
* 由VelocityServlet.init()调用,
* 在此找出模版的路径
*/
protected Properties loadConfiguration(ServletConfig config )
throws IOException, FileNotFoundException {
Properties p = new Properties();
//取得路径
String path = config.getServletContext().getRealPath("/");
if (path == null)
{
System.out.println(" SampleServlet.loadConfiguration() : unable to "
+ "get the current webapp root. Using '/'. Please fix.");
path = "/";
}
//设置路径
p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path);
return p;
}
/**
* Velocity主要的商业逻辑处理方法,由VelocityServlet自动调用
* @param ctx 模板上下文
* @return Template 模板信息
*/
public Template handleRequest( HttpServletRequest request,
HttpServletResponse response, Context ctx )
{
//主要在此设置演示用的数据,开发中在此调用相应的业务处理流程,
//并设置返回到页面的数据
//待展示的列表数据
String p1 = "第一位:LiuDong";
String p2 = "第二位:Liang.xf";
Vector personList = new Vector();
//中文需要转换
try {
personList.addElement(new String(p1.getBytes(), "ISO-8859-1") );
personList.addElement(new String(p2.getBytes(), "ISO-8859-1") );
} catch (Exception e) {
System.out.println("数据转换异常:"+e);
}
//设置数据,供页面模版替换成显示的数据
ctx.put("theList", personList );
//定义模板
Template outty = null;
try
{
//取模板
outty = getTemplate("example.html");
}
catch( ParseErrorException pee )
{
System.out.println("SampleServlet: parse error for template " + pee);
}
catch( ResourceNotFoundException rnfe )
{
System.out.println("SampleServlet: template not found " + rnfe);
}
catch( Exception e )
{
System.out.println("Error " + e);
}
return outty;
}
(3)在web.xml中的配置:
<web-app>
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>com.koal.velocity.SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/SampleServlet</url-pattern>
</servlet-mapping>
</web-app>
(4)环境的搭建
在lib目录内分别copy进:commons-collections.jar,velocity-1.4.jar,velocity-dept.jar;
Tomcat运行环境正常。
启动Tomcat,在IE上输入:http://localhost:8080/example,页面显示数据列表:
Here's the list of people
Names: |
第一位:LiuDong |
第二位:Liang.xf |