前不久,接触到.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())?
这个是直接输入到页面上如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
生成静态页--是的,这是让大家都心动的
下面的代码是我第一个练习:

usingusingusingusingusing///<summary>
//////</summary>
publicpartialclassprotectedvoid Page_Load(object//创建NVelocity引擎的实例对象
        VelocityEngine velocity =new//初始化该实例对象
        ExtendedProperties props =new ExtendedProperties();
        props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
          //可换成:props.AddProperty("resouce.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"//从文件中读取模板
        Template temp = velocity.GetTemplate("myTemplate.html"=new VelocityContext();
        context.Put("from", "Sichuan""to", "hainan""subject", "welcome to nvelocity""name", "McJeremy"//合并模板
        StringWriter writer =new//velocity.MergeTemplate(context, writer);
        temp.Merge(context, writer);
//输入
        Response.Write(writer.ToString().Replace("\r\n", "<br/>"
}
以下是生成静态页的练习:

usingusingusingusingusing///<summary>
//////</summary>
publicpartialclassprotectedvoid Page_Load(object//创建NVelocity引擎的实例对象
        VelocityEngine velocity =new//初始化该实例对象
        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"//从文件中读取模板
        Template temp = velocity.GetTemplate("myTemplate.html"=new VelocityContext();
        context.Put("from", "Sichuan""to", "hainan""subject", "welcome to nvelocity""name", "McJeremy"//合并模板
        StringWriter writer =new//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();
        }

posted on 2008-06-25 17:48 McJeremy&Fan 阅读(103) 评论(0)  编辑 收藏 所属分类: ASP.NET(C#)

[使用Ctrl+Enter键可以直接提交]
导航统计常用链接留言簿我的标签最新随笔搜索

支持,收藏 --金色海洋(jyk) 可能是你在页面某个地方重新定义了一个变量名len吧,否则就算你用typeof(len)也应该是function --孤蝶傲飞 究其原因是因为用来排序的列重复了,当数据正好重复在分界点上时,Access不知道任何取哪几条,于是它就把所有排序列重复的数据也全都带上了 --随风飘扬 --斧头帮少帮主 顶~!! --Lemmon Tea

阅读排行榜评论排行榜60天内阅读排行

【转】NVelocity模板引擎初学总结的更多相关文章

  1. NVelocity模板引擎学习笔记

    NVelocity模板引擎学习笔记 学习模板引擎有一段时间现在做一些总结

  2. Nvelocity模板引擎开发网页

    在ASP.NET网站开发中,我们要做许多的网页,如果多个网页的内容框架有些重复使用,我们用NVelocity模板引擎,就可以把相同的部分html代码单独放在一个文件中就行了,当要使用的时候,只需使用# ...

  3. Asp.net动态页面静态化之初始NVelocity模板引擎

    Asp.net动态页面静态化之初始NVelocity模板引擎 静态页面是网页的代码都在页面中,不须要运行asp,php,jsp,.net等程序生成client网页代码的网页,静态页面网址中一般不含&q ...

  4. 初见NVelocity模板引擎

    //using NVelocity.App; //using NVelocity; //using NVelocity.Runtime; VelocityEngine vltEngine = new ...

  5. NVelocity模板引擎的使用

    第一种使用方法直接赋值: VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants ...

  6. nvelocity模板引擎

    using NVelocity.App;using NVelocity.Runtime;using NVelocity; VelocityEngine vltEngine = new Velocity ...

  7. C#模板引擎NVelocity实战项目演练

    一.背景需求 很多人在做邮件模板.短信模板的时候,都是使用特殊标识的字符串进行占位,然后在后台代码中进行Replace字符串,如果遇到表格形式的内容,则需要在后台进行遍历数据集合,进行字符串的拼接,继 ...

  8. 模板引擎Nvelocity实例

    前言 最近一直忙于工作,没时间来管理博客,同时电脑也不给力,坏了一阵又一阵,最后还是去给修理了,这不刚一回来迫不及待的就写一篇文章来满足两个月未写博客的紧迫感. Nvelocity 关于nveloci ...

  9. ASP.NET 模板引擎 - NVelocity

    1,HTML的Form表单数据按Button提交数据以后,由 Action 指定的服务器端处理程序(.ashx)进行处理后 ,再响应的浏览器. 2,我们把 HTML的表单,写到 .ashx 一般处理程 ...

随机推荐

  1. 理解C++的inline函数

    C++的inline函数就是编译器在编译代码时,将"对此函数的每一个调用"都以函数本体替换之,该过程发生在编译期间. inline函数的优点是,它可以省去函数调用所带来的额外开销, ...

  2. oracle kill session

    kill session 是DBA经常碰到的事情之一.如果kill 掉了不该kill 的session,则具有破坏性,因此尽可能的避免这样的错误发生.同时也应当注意,如果kill 的session属于 ...

  3. [C#对sql操作]C#对sql server 2008数据库操作

    using System.Data; using System.Data.SqlClient SqlConnection conn = new SqlConnection(System.Configu ...

  4. Codeforces 732e [贪心][stl乱搞]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...

  5. http://blog.csdn.net/krislight/article/details/9391455

    http://blog.csdn.net/krislight/article/details/9391455

  6. 修改.gitignore后让其生效

    在使用git的时候我们有时候需要忽略一些文件或者文件夹.我们一般在仓库的根目录创建.gitignore文件在提交之前,修改.gitignore文件,添加需要忽略的文件.然后再做add commit p ...

  7. webpack 往右一点之 “你好,初次见面”

    webpack  模块打包器 模块化工具的目标: 将依赖树拆分成按需加载的块 初始化加载的耗时尽量少 各种静态资源都可以视作模块 将第三方库整合成模块 自定义打包逻辑 适合大项目

  8. CRM客户关系管理系统 北京易信软科信息技术有限公司

    北京易信软科信息技术有限公司 推出大型erp系统,库存管理系统,客户关系管理系统,车辆登记管理系统,员工管理系统,采购管理系统,销售管理系统,为您的企业提供最优质的产品服务 北京易信软科您可信赖的北京 ...

  9. 《JavaScript高级程序设计》学习笔记(2)--JS运算符详解

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 思维导图 前面对JS的运算符的操作很多细节的东西没有提及,今天给大家分享一张网上找的思维导图,对这一部 ...

  10. 后台使用Spring MVC 4.15 版本 通过 ajaxFileUpload plugin插件上传文件相应时引起的一个小问题,Chrome、Firefox中出现SyntaxError:unexpected token <

    html: 使用ajaxFileUpload插件做文件上传时,后台返回json格式的数据,js代码如下: 接下来,把结果错误信息打印出来: 先在网上找了下解决办法方案,stackoverflow上有说 ...