日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NVelocity标签使用详解

發(fā)布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。