NVelocity模板引擎初学总结。[zhuan]
2009-10-17 14:29
120 查看
前不久,接触到.NET下的MVC-MonoRail,它推荐使用的模板引擎就是NVelocity(目前由Castle Project项目组接手)
因此决定自学一下NVelocity的使用(抛开MonoRail)。
--
首先:在Castle Project上下载一个CastleProject包,我下载的是CastleProject-1.0-RC3.msi
安装后,在其下的bin目录中可找到NVelocity.dll(NET项目中将用到),并将其复制出来放到我的测试WEB/BIN目录下。
到castleproject上看了一下using it大致有四步:
先要引入以下名称空间:
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例
VelocityEngine velocity = new VelocityEngine(); //也可以使用带参构造函数直接实例。
ExtendedProperties props = new ExtendedProperties();
velocity.Init(props);
第二步:Creating the Template加载模板文件
这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
Template template = velocity.GetTemplate(@"path/to/myfirsttemplate.vm");
第三步:Merging the template整合模板
VelocityContext context = new VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject", "Welcome to NVelocity");
context.Put("customer", new Customer("John Doe") );
第四步:创建一个IO流来输出模板内容。推荐使用StringWriter(因为template中以string形式存放)
StringWriter writer = new StringWriter();
template.Merge(context, writer);
Response.Write(writer.ToString());
---通过上述步骤就可以轻松的使用NVelocity模板引擎的技术了。
有没有发现最后的Response.Write(writer.ToString())?
这个是直接输入到页面上。如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
生成静态页--是的,这是让大家都心动的。
下面的代码是我第一个练习:
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
public partial class NVelocity_模板引擎测试 : System.Web.UI.Page
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
public partial class NVelocity_模板引擎测试 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建NVelocity引擎的实例对象
VelocityEngine velocity = new VelocityEngine();
//初始化该实例对象
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
velocity.Init(props);
//从文件中读取模板
Template temp = velocity.GetTemplate("myTemplate.html");
IContext context = new VelocityContext();
context.Put("from", "Sichuan");
context.Put("to", "hainan");
context.Put("subject", "welcome to nvelocity");
context.Put("name", "McJeremy");
//合并模板
StringWriter writer = new StringWriter();
//velocity.MergeTemplate(context, writer);
temp.Merge(context, writer);
//生成静态页
using (StreamWriter writer2 = new StreamWriter(Server.MapPath("/") + "test.html"), false, Encoding.UTF8, 200))
{
writer2.Write(writer);
writer2.Flush();
writer2.Close();
}
}
}
因此决定自学一下NVelocity的使用(抛开MonoRail)。
--
首先:在Castle Project上下载一个CastleProject包,我下载的是CastleProject-1.0-RC3.msi
安装后,在其下的bin目录中可找到NVelocity.dll(NET项目中将用到),并将其复制出来放到我的测试WEB/BIN目录下。
到castleproject上看了一下using it大致有四步:
先要引入以下名称空间:
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例
VelocityEngine velocity = new VelocityEngine(); //也可以使用带参构造函数直接实例。
ExtendedProperties props = new ExtendedProperties();
velocity.Init(props);
第二步:Creating the Template加载模板文件
这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
Template template = velocity.GetTemplate(@"path/to/myfirsttemplate.vm");
第三步:Merging the template整合模板
VelocityContext context = new VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject", "Welcome to NVelocity");
context.Put("customer", new Customer("John Doe") );
第四步:创建一个IO流来输出模板内容。推荐使用StringWriter(因为template中以string形式存放)
StringWriter writer = new StringWriter();
template.Merge(context, writer);
Response.Write(writer.ToString());
---通过上述步骤就可以轻松的使用NVelocity模板引擎的技术了。
有没有发现最后的Response.Write(writer.ToString())?
这个是直接输入到页面上。如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
生成静态页--是的,这是让大家都心动的。
下面的代码是我第一个练习:
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
public partial class NVelocity_模板引擎测试 : System.Web.UI.Page
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
public partial class NVelocity_模板引擎测试 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建NVelocity引擎的实例对象
VelocityEngine velocity = new VelocityEngine();
//初始化该实例对象
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
velocity.Init(props);
//从文件中读取模板
Template temp = velocity.GetTemplate("myTemplate.html");
IContext context = new VelocityContext();
context.Put("from", "Sichuan");
context.Put("to", "hainan");
context.Put("subject", "welcome to nvelocity");
context.Put("name", "McJeremy");
//合并模板
StringWriter writer = new StringWriter();
//velocity.MergeTemplate(context, writer);
temp.Merge(context, writer);
//生成静态页
using (StreamWriter writer2 = new StreamWriter(Server.MapPath("/") + "test.html"), false, Encoding.UTF8, 200))
{
writer2.Write(writer);
writer2.Flush();
writer2.Close();
}
}
}
相关文章推荐
- NVelocity模板引擎初学总结
- 关于一些排序算法的个人总结(初学)
- 【总结】初学js注意的知识点
- struts 2初学和常见的代码调试 报错总结
- 初学一周犯错经验总结
- 初学 异常总结,异常finally执行顺序问题
- 初学VC+数据库的总结
- Android初学习 - 基础&进阶总结
- java界面初学组件肤浅小总结
- java初学 中国象棋 总结
- 菜鸟之C语言初涉(初学编程之总结系列之二)
- 初学RDF的一些总结
- 5月5日初学java知识点总结
- 初学SpringMVC 小总结(有些术语说的不准确以后会加强,有些只是我个人理解,发现有问题可以告诉我)
- 初学前端阶段性的自我总结
- Mybatis初学使用方法总结
- 初学Java Web(4)——Servlet学习总结
- 初学Flash的积累(一周一总结)提高以后开发速度---呵呵 我记不住这些东东
- 初学Linux内核模块及各种总结(有助于初学者使用)
- 【转】NVelocity模板引擎初学总结