NVelocity标签使用详解
生活随笔
收集整理的這篇文章主要介紹了
NVelocity标签使用详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文使用的NVelocity版本為1.1.1,應該是目前為止最新的版本吧,前幾天在google上找了一個自稱是NVelocity 1.6.1 bate2的dll,下載下來一看更新時間是2009年的,還沒版本NVelocity?1.1.1(2010年出的) 新呢!
本文目錄:一、資源、文檔下載。
二、使用步驟。
三、代碼演示。
一、資源、文檔下載:
官方下載地址與參考文檔
其他下載地址(版本比較低不建議下載)
本文NVelocity 1.1.1 dll與示例下載
NVelocity 使用文檔下載
二、使用步驟。
a) 創(chuàng)建Velocity 引擎(VelocityEngine)并設置屬性.
b)?VelocityContext 上下文對象創(chuàng)建于設置.
c) 使用VelocityEngine(Velocity 引擎)創(chuàng)建模板(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.創(chuàng)建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();
// 頁面參數(shù)設值
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.創(chuàng)建模板(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 1.1.1 dll與示例下載
【Stone 制作整理,引用請寫明出處謝謝合作,聯(lián)系QQ:1370569】
總結
以上是生活随笔為你收集整理的NVelocity标签使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows重装Anaconda3失败
- 下一篇: current of 使用