NVelocity标签使用详解
2011-09-14 18:33
316 查看
本文使用的NVelocity版本为1.1.1,应该是目前为止最新的版本吧,前几天在google上找了一个自称是NVelocity 1.6.1 bate2的dll,下载下来一看更新时间是2009年的,还没版本NVelocity 1.1.1(2010年出的) 新呢!
一、资源、文档下载:
官方下载地址与参考文档
其他下载地址(版本比较低不建议下载)
本文NVelocity 1.1.1 dll与示例下载
NVelocity 使用文档下载
二、使用步骤。
a) 创建Velocity 引擎(VelocityEngine)并设置属性.
b) VelocityContext 上下文对象创建于设置.
c) 使用VelocityEngine(Velocity 引擎)创建模板(Template).
d) 合并模板和上下文对象、输出.
三、代码演示。
先引入NVelocity.dll,然后添加代码。
1.一般处理类ShowHTML.ashx代码如下:
2.default.html模板代码如下:
3.效果如下:
本文目录:
一、资源、文档下载。
二、使用步骤[b]。 [/b]
三、代码演示。
一、资源、文档下载:
官方下载地址与参考文档
其他下载地址(版本比较低不建议下载)
本文NVelocity 1.1.1 dll与示例下载
NVelocity 使用文档下载
二、使用步骤。
a) 创建Velocity 引擎(VelocityEngine)并设置属性.
b) VelocityContext 上下文对象创建于设置.
c) 使用VelocityEngine(Velocity 引擎)创建模板(Template).
d) 合并模板和上下文对象、输出.
三、代码演示。
先引入NVelocity.dll,然后添加代码。
1.一般处理类ShowHTML.ashx代码如下:
<%@ WebHandler Language="C#" Class="ShowHTML" %> using System; using System.Web; // NVelocity 引用 using NVelocity; using NVelocity.App; using NVelocity.Runtime; public class ShowHTML : IHttpHandler { public void ProcessRequest(HttpContext context) { // 1.创建Velocity 引擎(VelocityEngine)并设置属性 VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // Velocity加载类型 velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, // Velocity加载文件路径 context.Server.MapPath("~/Template/")); velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312"); // 输入编码格式设置 velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312"); // 输出编码格式设置 velocityEngine.Init(); // 2.Velocity 上下文对象设置 VelocityContext vc = new VelocityContext(); // 页面参数设值 vc.Put("Name", "MT!"); System.Collections.Generic.List<String> list = new System.Collections.Generic.List<string>(); for (int i = 1; i < 11; i++) { list.Add("My Name Is :" + i); } vc.Put("list", list); // 3.创建模板(Template) Template template = velocityEngine.GetTemplate("default.html"); // 4.合并模板和上下文对象、输出 template.Merge(vc, HttpContext.Current.Response.Output); HttpContext.Current.Response.End(); } public bool IsReusable { get { return false; } } }
2.default.html模板代码如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>NVelocity 使用测试模板</title> </head> <body> 俺叫$Name <br /> #foreach($item in $list) $item<br /> #end </body> </html>
3.效果如下:
相关文章推荐
- NVelocity标签使用详解
- NVelocity标签使用详解
- struts2标签使用详解
- HTML中meta 标签使用详解
- 详解head头标签元素的意义以及使用场景
- HTML中的<meta>标签的使用详解
- HTML中javascript的<script>标签使用方法详解
- Mybatis 框架使用的最核心内容(二):mapper.xml中常用的标签详解
- Meta标签使用详解
- c:foreach标签使用详解
- Java的“Goto”与标签及使用详解
- 详解head头标签元素的意义以及使用场景
- Mybatis 框架使用的最核心内容(二):mapper.xml中常用的标签详解
- Mybatis 框架使用的最核心内容(二):mapper.xml中常用的标签详解
- struts2标签使用详解
- 详解XHTML中的标题标签与段落标签的使用
- Android XML shape 标签使用详解
- struts2中s:iterator 标签的使用详解
- NVelocity标签使用详解
- HTML embed标签使用方法和属性详解