一个关于Velocity的例子
关键字: velocity自己做的一个关于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
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;
}
}
***********************************
本例子是参照网上搜集的资料而形成的,现提出来供大家参考,谢谢
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 26631 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
共 8 张
最近加入圈子
链接
最新评论
-
一个jfreechart的例子
kan kan 我的项目正需要
-- by discovery12 -
真正的用window.open()代 ...
今天也在搞这问题,最后还是放弃open() "兼容FF,IE等多种浏览器" 你不 ...
-- by icefire -
DWR又一例级联(省份-城 ...
我想问下,为什么不用dwr中util.js的方法来生成呢?不是更好用点吗,而且代 ...
-- by hxdream -
一个关于ajax的例子(proto ...
hehe谢了
-- by shiren1118 -
一个关于DWR的例子
Run 写道 <script type='text/javascript ...
-- by sway






评论排行榜