模板语言velocity项目例子
来源:互联网 发布:sql 2005 32位 下载 编辑:程序博客网 时间:2024/06/05 06:50
模板语言很简单,这里只是提一下
index.vm文件:
<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>
后台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", "serial" })
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 = "啊啊啊啊啊啊啊啊";
String p2 = "啊啊啊啊啊啊啊啊";
String p3 = "啊啊啊啊啊啊啊啊y";
String p4 = "啊啊啊啊啊啊啊啊";
Vector<String> personList = new Vector<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;
}
}
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>velocity</display-name>
<servlet>
<servlet-name>velocityServlet</servlet-name>
<servlet-class>com.velocity.MyVelocityServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/classes/velocity.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>velocityServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
</welcome-file-list>
</web-app>
- 模板语言velocity项目例子
- 关于模板语言Velocity的简单例子
- 模板语言:Velocity简介
- 模板语言velocity
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- Velocity模板(VM)语言介绍
- 前端模板语言velocity选择
- Velocity模板(VM)语言介绍
- Velocity模板语言(VTL):说明
- Velocity模板项目使用案例
- Velocity模板(VM)语言介绍、Velocity模板使用指南
- Java语言介绍(04)开源项目(04)模板引擎(01)Velocity
- Velocity模板语言(VM)介绍
- Velocity在web项目中生成模板
- 电子工程师的出路
- NSString加密,解密和64编码
- 转:豆瓣为什么短期内会这么成功?
- 西蒙iphone-OpenGL ES 教程-03
- OPENFILENAME示例代码
- 模板语言velocity项目例子
- oracle 装了三遍终于明白了
- 自学WPF--第八课FlowDocument
- java context解释
- Android画图之抗锯齿
- js实现各种进制的转换
- Quartz调度器的使用001
- PHP程序员应该使用的10个组件
- 在winsows下编写的shell脚本,传到lunux下出错