博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NVelocity标签使用详解
阅读量:4603 次
发布时间:2019-06-09

本文共 2166 字,大约阅读时间需要 7 分钟。

本文使用的NVelocity版本为1.1.1,应该是目前为止最新的版本吧,前几天在google上找了一个自称是NVelocity 1.6.1 bate2的dll,下载下来一看更新时间是2009年的,还没版本NVelocity 1.1.1(2010年出的) 新呢!

本文目录:
  一、资源、文档下载。   
  二、使用步骤。   
  三、代码演示。

一、资源、文档下载:

  

  

  

  

二、使用步骤。

  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
list = new System.Collections.Generic.List
(); 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模板代码如下:

  NVelocity 使用测试模板       俺叫$Name 
#foreach($item in $list) $item
#end

3.效果如下:

【Stone 制作整理,引用请写明出处谢谢合作,联系QQ:1370569】

转载于:https://www.cnblogs.com/vipstone/archive/2011/09/09/2172341.html

你可能感兴趣的文章
OpenMobile's Application Compatibility Layer (ACL)
查看>>
竞价广告系统-广告检索
查看>>
强哥PHP面向对象学习笔记
查看>>
[转]基于.NET平台常用的框架整理
查看>>
Symbian (Read Inbox)读取收件箱的内容
查看>>
良好的编程规范
查看>>
struts2 入门
查看>>
.net 编译原理
查看>>
mean 快速开发和现有技术的对比分析
查看>>
Metro Style app :浏览器扩展
查看>>
linux的kernel是怎样工作的(TI_DM36X_ARM系统)(1)
查看>>
[luogu4310] 绝世好题 (递推)
查看>>
[luogu3203 HNOI2010] 弹飞绵羊 (分块)
查看>>
-Dmaven.multiModuleProjectDirectory system propery is not set.
查看>>
Python2 unichr() 函数
查看>>
Python 字典 copy()方法
查看>>
Minimum Path Sum
查看>>
Remove Duplicates from Sorted Array II
查看>>
常量指针和指针常量巧妙记忆方法[转]
查看>>
python-haproxy作业讲解视频总结
查看>>