自己做的一个关于velocity的例子。
index.vm
---------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Velocity Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="velocity,模板">
</head>
<body >
#set($name1="first!")
Hello, $name1 <br>
Hello, $name2
<br/>
Hello, $name3
<br/>
Hello, $name3
<hr>
<table border='1' width='200' >
<tr>
<td>
yy
</td>
</tr>
#foreach ($iii in $theList)
<tr>
<td bgcolor="#eeeeee">$iii</td>
</tr>
#end
</table>
</body>
</html>
************************
MyVelocityServlet.java
-----------------------------------------------------------------------
package com.velocity;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
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.app.VelocityEngine;
import org.apache.velocity.VelocityContext;
@SuppressWarnings("deprecation")
public class MyVelocityServlet extends VelocityServlet {
protected Properties loadConfiguration(ServletConfig config)
throws IOException, FileNotFoundException {
VelocityEngine engine = new VelocityEngine();
Properties p = new Properties();
String path = config.getServletContext().getRealPath("/");
if (path == null) {
path = "/";
}
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
p.setProperty("runtime.log", path + "velocity.log");
try {
engine.init(p); // 载入模板的路径path ,即上下文路径
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
public Template handleRequest(HttpServletRequest request,
HttpServletResponse response, Context ctx) {
try {
request.setCharacterEncoding("UTF8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setCharacterEncoding("UTF8");
Template template = null;
Template template2 = null;
try {
/**
* 主要代码
*/
Velocity.init();
// VelocityContext context = new VelocityContext();
String p1 = "JAVA";
String p2 = "C++";
String p3 = "Ruby";
String p4 = "D";
Vector<string></string> personList = new Vector<string></string>();
personList.addElement(p1);
personList.addElement(p2);
personList.addElement(p3);
personList.addElement(p4);
/**
* 将模板数据name, list 放置到上下文环境 context 中去
*/
ctx.put("name2", " 这里是在后台赋值! ");
ctx.put("name3", " 小齐! ");
ctx.put("theList", personList);
template = Velocity.getTemplate("/index.vm");
} catch (Exception e) {
e.printStackTrace();
}
// 以下一段代码主要是获得模板的HTML内容 在后台显示
try {
template2 = Velocity.getTemplate("/index.vm");
VelocityContext context = new VelocityContext();
context.put("name2", "这里在后台第二次赋值!");
StringWriter writer = new StringWriter();
template2.merge(context, writer);
System.out.println(writer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return template;
}
}
***********************************
本例子是参照网上搜集的资料而形成的,现提出来供大家参考,谢谢