【转】NVelocity模板引擎初学总结
因此决定自学一下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())?
这个是直接输入到页面上如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
生成静态页--是的,这是让大家都心动的
下面的代码是我第一个练习:
//////</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/>"
}
以下是生成静态页的练习:
//////</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#)
阅读排行榜评论排行榜60天内阅读排行
【转】NVelocity模板引擎初学总结的更多相关文章
- NVelocity模板引擎学习笔记
NVelocity模板引擎学习笔记 学习模板引擎有一段时间现在做一些总结
- Nvelocity模板引擎开发网页
在ASP.NET网站开发中,我们要做许多的网页,如果多个网页的内容框架有些重复使用,我们用NVelocity模板引擎,就可以把相同的部分html代码单独放在一个文件中就行了,当要使用的时候,只需使用# ...
- Asp.net动态页面静态化之初始NVelocity模板引擎
Asp.net动态页面静态化之初始NVelocity模板引擎 静态页面是网页的代码都在页面中,不须要运行asp,php,jsp,.net等程序生成client网页代码的网页,静态页面网址中一般不含&q ...
- 初见NVelocity模板引擎
//using NVelocity.App; //using NVelocity; //using NVelocity.Runtime; VelocityEngine vltEngine = new ...
- NVelocity模板引擎的使用
第一种使用方法直接赋值: VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants ...
- nvelocity模板引擎
using NVelocity.App;using NVelocity.Runtime;using NVelocity; VelocityEngine vltEngine = new Velocity ...
- C#模板引擎NVelocity实战项目演练
一.背景需求 很多人在做邮件模板.短信模板的时候,都是使用特殊标识的字符串进行占位,然后在后台代码中进行Replace字符串,如果遇到表格形式的内容,则需要在后台进行遍历数据集合,进行字符串的拼接,继 ...
- 模板引擎Nvelocity实例
前言 最近一直忙于工作,没时间来管理博客,同时电脑也不给力,坏了一阵又一阵,最后还是去给修理了,这不刚一回来迫不及待的就写一篇文章来满足两个月未写博客的紧迫感. Nvelocity 关于nveloci ...
- ASP.NET 模板引擎 - NVelocity
1,HTML的Form表单数据按Button提交数据以后,由 Action 指定的服务器端处理程序(.ashx)进行处理后 ,再响应的浏览器. 2,我们把 HTML的表单,写到 .ashx 一般处理程 ...
随机推荐
- Easyui 小脚本
function addTab(subtitle, url, icon) { if (!$('#tabs').tabs('exists', subtitle)) { $('#tabs').tabs(' ...
- TortoiseSvn客户端出现Http state 405 'Method Not Allowed' 的解决办法
原文地址链接 http://stackoverflow.com/questions/28247512/visualsvn-server-3-2-unexpected-http-status-405 就 ...
- matlab工具箱之人眼检测+meanshift跟踪算法--人眼跟踪
Viola-Jones 人眼检测算法+meanshift跟踪算法 这次的代码是对视频中的人眼部分进行检测加跟踪,检测用的是matlab自带的人眼检测工具箱 下面是matlab官网介绍这个算法的一些东西 ...
- nyoj 82 迷宫寻宝(二)
http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...
- Study plan for automation test framework
虽然部门的automation建立起来有两年多,去年项目一直很忙,仅限于应用(e.g 运行脚本测试或者写一些简短的测试脚本),但是一直没有深入研究其组成框架.近期希望抽出时间来做深入学习. 初步计划从 ...
- 转ASP.NET1.1请求队列限制
在教务web的选课的维护中,经常面临asp.net1.1报错,在客户端跳转到用户自定义页面,在服务器端可以看到如下错误信息: “/”应用程序中的服务器错误. 服务器太忙 说明: 执行当前 Web 请求 ...
- 通过创建临时表合并hive小文件
#!/bin/bash #set -x DB=$1 #获取hive表定义 ret=$(hive -e "use ${DB};show tables;"|grep -v _es|gr ...
- Norflash控制器的Verilog建模之二(仿真)
前言:经过几天修改,norflash控制器基本已经完成,通过仿真.完整的norflash包含2个模块:直接操作硬件的norflash_ctrl.v与控制ctrl模块的驱动norflash_driver ...
- .NET程序集的编译目标平台:X86 &AnyCPU &X64
在我们测试平台上发布客户端组件,经常会碰到因为build的版本是x86还是anycpu而引起的application error的问题.借此,研究了一下X86,X64,AnyCPU的区别. 使用.ne ...
- 前端学习实践笔记--JavaScript深入【2】
趁热继续再来学习一波,接下来主要介绍函数,object,数组,面向对象,new实例化. 在介绍“对象”之前,首先得梳妆打扮一番吧,那这梳妆打扮主要有两条路线,一条是淑女范(利用函数对象化),一条是邻家 ...