NVelocity是java velocity的c#实现,目前我在CodePlex维护着与velocity同步的版本。NVelocity也在项目中使用着,在社区也有国外开发者的一些反馈。
下面是一个在Asp.Net如何使用NVelocity的非常简单例子:
定义HttpHandler:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
namespace NVelocity.TestWebsite
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
using System;
4
using System.Collections.Generic;
5
using System.IO;
6
using System.Web;
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
using Commons.Collections;
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
using NVelocity.App;
11
using NVelocity.Context;
12
using NVelocity.Runtime;
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
///
16
/// </summary>
17
public class NVelocityHandler : IHttpHandler
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IHttpHandler Members#region IHttpHandler Members
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public bool IsReusable
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return false; }
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
public void ProcessRequest(HttpContext context)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
VelocityEngine velocity = new VelocityEngine();
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
ExtendedProperties props = new ExtendedProperties();
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
//定义模板路径
33
props.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views"));
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
//初始化
36
velocity.Init(props);
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
List<City> list = new List<City>();
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
list.Add(new City()
{ Name = "sh", Id = 21 });
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
list.Add(new City()
{ Name = "bj", Id = 22 });
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
list.Add(new City()
{ Name = "tj", Id = 23 });
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
IContext c = new VelocityContext();
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
//添加到上下文中
48
c.Put("cities", list);
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
//根据请求输出
51
velocity.MergeTemplate(context.Request.QueryString["vm"] + ".vm", "UTF-8", c, context.Response.Output);
52
}
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
#endregion
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
58
/// 城市
59
/// </summary>
60
public class City
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
63
/// ID
64
/// </summary>
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Id
{ get; set; }
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
68
/// 名称
69
/// </summary>
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
71
}
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一个用于测试的default.vm模板文件:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
##循环输出
2
#foreach($city in $cities)
3
Id:$city.id<br />
4
城市名称:$city.name<br />
5
#end
6
##索引获取数据
7
$cities.get_item(2).name
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在Web.config中配置上面定义的HttpHandler:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<httpHandlers>
<add verb="*" path="*.page" type="NVelocity.TestWebsite.NVelocityHandler,NVelocity.TestWebsite"/>
</httpHandlers>
请求及输出效果:
![](https://images.cnblogs.com/cnblogs_com/terryliang/nvelocity.jpg)