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

2,我们把 HTML的表单,写到 .ashx 一般处理程序页面中,这样就一般处理程序页面就可以显示 Form表单登陆,并且可以处理是否登陆成功的逻辑部分,

 也就是把前台显示和后天业务逻辑拼到了一起,如下:

 public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //渲染一个Form表单
context.Response.Write("<html>");
context.Response.Write("<head>一般处理程序登陆页面</head><body><form action='login.ashx' action='get'><br/><br/>"); //
context.Response.Write("用户名:<input type='text' name='UserName' /><br/>");
context.Response.Write("密码:<input type='password' name='Pwd' /><br/>");
context.Response.Write("提交:<input type='submit' /><br/>"); string strUserName = context.Request.QueryString["UserName"];
string strPwd = context.Request.QueryString["Pwd"];
if (string.IsNullOrEmpty(strUserName) == false) //登陆名不为空的时候,检查密码是否正确
{
if (strUserName == "admin" && strPwd == "")
{
context.Response.Write("恭喜您,登陆成功");
}
else
{
context.Response.Write("登陆失败");
}
}
}

  但是这样去做,网页表单样式等 只有通过这种修改 .ashx一般处理程序的方法去修改。

  没法给前台美工可以处理的地方,另外如果后天业务逻辑复杂的话,这样的整合就更复杂了。

  如果有一个文件 又可以区分前台显示和后台业务逻辑 这样就好了。

3,nVelocity是一个基于.NET的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定义的对象

4,nVelocity,下载网址:NVelocity - A .Net Template Engine ,下载后把 dll文件放到项目中,然后 引用添加对dll的引用,注意对应的 .NET Framework版本

6, Login_NVelocity.html和Login_NVelocity.ashx代代码分别如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>一般处理程序登陆页面</title>
</head>
<body>
<form action="Login_NVelocity.ashx" method="get">
用户名:<input type="text" name="UserName" value="$name" /><br />
密码:<input type="password" name="password" value="$pwd" /><br />
提交:<input type="submit" /><br />
<p>$message</p>
</form>
</body>
</html>

Login_NVelocity.ashx :  一般处理程序页面实现了对HTML模板页面中变量的控制  $name, $pwd,$message

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO; namespace HttpHandler
{
/// <summary>
/// LoginNVelocity 的摘要说明
/// </summary>
public class LoginNVelocity : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; string strUserName = context.Request["username"];
string strPwd = context.Request["password"];
string strMsg = context.Request["message"]; string strHtml = "";
if (string.IsNullOrEmpty(strUserName) && string.IsNullOrEmpty(strPwd))
{
strHtml = Template_Nvelocity("", "", "");
}
else if (strUserName == "admin" && strPwd == "")
{
strMsg = "恭喜您,登陆成功!";
context.Response.Write(strMsg);
}
else
{
strMsg = "登陆失败,用户名或者密码错误";
strHtml = Template_Nvelocity(strUserName, strPwd, strMsg);
}
//输出
context.Response.Write(strHtml); }
//模板引擎方法,传递 登录名、登陆密码、登陆是否成功 这三个变量
public string Template_Nvelocity(string strUserName, string strPwd, string strMsg)
{ //创建NVlecocity模板引擎的实例对象
VelocityEngine vlEngine = new VelocityEngine(); //初始化实例对象,定义对象的部分属性
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/")); //指定模板文件所在文件夹
vlEngine.Init(props); //替换模板中的数据
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("data", data); //设置参数,在模板中可以通过$Data读取数据
vltContext.Put("name", strUserName);
vltContext.Put("pwd", strPwd);
vltContext.Put("message", strMsg); //从文件中读取模板
Template VltTemp = vlEngine.GetTemplate("Login_NVelocity.html");
//合并模板
StringWriter writer = new StringWriter();
VltTemp.Merge(vltContext, writer); //转化为字符串
string html = writer.GetStringBuilder().ToString(); return html;
} public bool IsReusable
{
get
{
return false;
}
}
}
}

------------------------------------------------gif动画演示--------------------------------------------------------

7,项目中新添加一个Person类,我们就可以通过NVolecity 把Person类的属性 在模板中进行调用、显示

  A.添加Person类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace HttpHandler
{
public class Person
{
public string Name { get; set; } //姓名
public int Age { get; set; } //年龄 public Person Father { get; set; } //父亲
}
}

B.添加一般处理程序 Person_Class_NVelocity.ashx ,把Person对象的属性 通过模板引擎 传递到Person.html页面中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO; namespace HttpHandler
{
/// <summary>
/// Person_Class_NVelocity 的摘要说明
/// </summary>
public class Person_Class_NVelocity : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; Person me = new Person();
me.Name = "Peter";
me.Age = ; Person Dad = new Person();
Dad.Name = "Da Peter";
Dad.Age = ; me.Father = Dad; string strHtml = Template_Nvelocity_P(me); //输出
context.Response.Write(strHtml);
} //模板引擎方法
public string Template_Nvelocity_P(Person per)
{ //创建NVlecocity模板引擎的实例对象
VelocityEngine vlEngine = new VelocityEngine(); //初始化实例对象,定义对象的部分属性
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/")); //指定模板文件所在文件夹
vlEngine.Init(props); //替换模板中的数据
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("data", data); //设置参数,在模板中可以通过$Data读取数据
vltContext.Put("p", per); //从文件中读取模板
Template VltTemp = vlEngine.GetTemplate("Person.html");
//合并模板
StringWriter writer = new StringWriter();
VltTemp.Merge(vltContext, writer); //转化为字符串
string html = writer.GetStringBuilder().ToString(); return html; } public bool IsReusable
{
get
{
return false;
}
}
}
}

  C.Person.html页面中直接调用传递过来的Person对象的属性

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
父亲的年龄是: $p.Father.Age &nbsp; 父亲的姓名: $p.Father.Name<br />
我的年龄是: $p.Age &nbsp; 我的姓名: $p.Name<br />
</body>
</html>

----------------------------------------------Gif动画演示----------------------------------------------------------

8, HTML中直接通过 对象的索引访问对象

  A.添加City_Class_NVelocity.ashx一般处理程序,定义Diationary 键值对并设置对应的值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO; namespace HttpHandler
{
/// <summary>
/// City_Class_NVelocity 的摘要说明
/// </summary>
public class City_Class_NVelocity : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //定义键值对
Dictionary<string, string> dict = new Dictionary<string, string>();
dict["BJ"] = "北京";
dict["SH"] = "上海";
dict["GZ"] = "广州"; string strHtml = Template_Nvelocity_C(dict); context.Response.Write(strHtml);
} //模板引擎方法
public string Template_Nvelocity_C(Dictionary<string, string> dict)
{ //创建NVlecocity模板引擎的实例对象
VelocityEngine vlEngine = new VelocityEngine(); //初始化实例对象,定义对象的部分属性
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/")); //指定模板文件所在文件夹
vlEngine.Init(props); //替换模板中的数据
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("data", data); //设置参数,在模板中可以通过$Data读取数据
vltContext.Put("c", dict); //从文件中读取模板
Template VltTemp = vlEngine.GetTemplate("City.html");
//合并模板
StringWriter writer = new StringWriter();
VltTemp.Merge(vltContext, writer); //转化为字符串
string html = writer.GetStringBuilder().ToString(); return html; } public bool IsReusable
{
get
{
return false;
}
}
}
}

  B.添加City.html, html中通过$c.BJ 来访问 .ashx中定义的对象属性

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
索引获得一般处理程序中的键、值对:<br />
c.BJ= $c.BJ<br />
c.SH= $c.SH<br />
c.GZ= $c.GZ<br />
</body>
</html>

----------------------------------------------Gif动画演示----------------------------------------------------------

9,对集合进行遍历   #foreach($item in $list)  ... #end

City_Class_NVelocity.ashx 中添加 string 数组 和 泛型后,在HTML模板中 遍历读取属性值:

  分别更新,City_Class_NVelocity.ashx和City.html如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Runtime;
using System.IO; namespace HttpHandler
{
/// <summary>
/// City_Class_NVelocity 的摘要说明
/// </summary>
public class City_Class_NVelocity : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; //定义键值对
Dictionary<string, string> dict = new Dictionary<string, string>();
dict["BJ"] = "北京";
dict["SH"] = "上海";
dict["GZ"] = "广州"; string strHtml = Template_Nvelocity_C(dict); context.Response.Write(strHtml);
} //模板引擎方法
public string Template_Nvelocity_C(Dictionary<string, string> dict)
{ //定义数组
string[] strs = new string[] { "字符串11111", "字符串22222", "字符串33333" };
//定义泛型 Person对象
List<Person> persons = new List<Person>();
persons.Add(new Person { Name = "习大大", Age = });
persons.Add(new Person { Name = "彭妈妈", Age = }); //创建NVlecocity模板引擎的实例对象
VelocityEngine vlEngine = new VelocityEngine(); //初始化实例对象,定义对象的部分属性
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/")); //指定模板文件所在文件夹
vlEngine.Init(props); //替换模板中的数据
VelocityContext vltContext = new VelocityContext();
//vltContext.Put("data", data); //设置参数,在模板中可以通过$Data读取数据
vltContext.Put("c", dict);
//添加数组和泛型参数
vltContext.Put("str", strs);
vltContext.Put("per", persons); //从文件中读取模板
Template VltTemp = vlEngine.GetTemplate("City.html");
//合并模板
StringWriter writer = new StringWriter();
VltTemp.Merge(vltContext, writer); //转化为字符串
string html = writer.GetStringBuilder().ToString(); return html; } public bool IsReusable
{
get
{
return false;
}
}
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
索引获得一般处理程序中的键、值对:<br />
c.BJ= $c.BJ<br />
c.SH= $c.SH<br />
c.GZ= $c.GZ<br /> <br /> foreace遍历读取字符串数组中的数据:
<ul>
#foreach($s in $str)
<li>$s</li>
#end
</ul>
<br />
foreace遍历读取泛型Person中的数据:
<ul>
#foreach($p in $per)
<li>$p.Name的年龄是$p.Age</li>
#end
</ul>
</body>
</html>

----------------------------------------------Gif动画演示----------------------------------------------------------

10,条件语句: #if() #else if() #end ,并且可以嵌套到ForEach中, 例如把上面city.html一段foreach代码中添加 if条件语句:

 <ul>
#foreach($p in $per)
#if($p.Age>30)
<li style="color: red">$p.Name的年龄是$p.Age</li>
#else
<li style="color: black">$p.Name的年龄是$p.Age</li>
#end
#end
</ul>

11,模板中调用其他模板 用 #include

  A. 项目中添加两个模板文件: head.html 和 foot.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<b>网站Head部分</b><br /><br />
<b>网站底部Foot部分,版权声明</b>
</body>
</html>

  B. #include("head.html") 这样就可以引用模板文件了

#include("head.html")

    索引获得一般处理程序中的键、值对:<br />
c.BJ= $c.BJ<br />
c.SH= $c.SH<br />
c.GZ= $c.GZ<br /> <br /> foreace遍历读取字符串数组中的数据:
<ul>
#foreach($s in $str)
<li>$s</li>
#end
</ul>
<br />
foreace遍历读取泛型Person中的数据:
<ul>
#foreach($p in $per)
#if($p.Age>30)
<li style="color: red">$p.Name的年龄是$p.Age</li>
#else
<li style="color: black">$p.Name的年龄是$p.Age</li>
#end
#end
</ul> #include("foot.html")

12,#parse, 除了实现调用其他模板功能以外 还可以解析NVelocity中的对象的属性。

  A. 更新Head.html , 直接调用.ashx中的对象

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<b>网站Head部分</b><br />
<b>Parse,除了实现Include的包含功能,还可以解析 NVelocity中的元素的值,如 c.BJ=$c.BJ</b> <br /> <br />

  B.city.html中即可显示出对象属性值:

总结: 以上为NVelocity的用法,以及HTML模板中对一般处理程序中对象调用方法。

附件: NVelocity DLL    Demo下载

ASP.NET 模板引擎 - NVelocity的更多相关文章

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

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

  2. 模板引擎Nvelocity实例

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

  3. 好用的模板引擎NVelocity

    CastleNVelocity-1.1.1,使用方法: 把dll放到项目中,添加引用,修改配置的文件夹以及数据模型,最后在逻辑代码中调用即可. 封装到CommonHelper.cs using Sys ...

  4. asp .net 模板引擎 使用 Razor 生成html静态页面

    刚开始不是理解 写完之后 觉得还蛮简单的 分为这几个步骤 1.获取页面模板Html 2.获取数据 3.解析模板和数据,生成静态页Html代码 4.生成静态文件 模板形式是mvc的模式,会mvc 看一下 ...

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

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

  6. 【转】NVelocity模板引擎初学总结

    转自:http://sunxitao88.blog.163.com/blog/static/68314439200861963326251/ 前不久,接触到.NET下的MVC-MonoRail,它推荐 ...

  7. Nvelocity模板引擎开发网页

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

  8. Asp.net MVC Razor模板引擎技巧分享

    Razor是Asp.net MVC中新的默认模板类型, 语法简单易用.这篇文章不涉及Razor的语法,主要介绍Razor的一些在MVC项目中的使用技巧,以及脱离MVC环境下,如何使用Razor. 阅读 ...

  9. 关于html、asp、php模板引擎、aspnet mvc、REST的一点思考

    先看我对REST的一点认识,下面是<rest实战> 这本书的序言文字:      在我刚刚开始从事解决计算问题的时候,业界就有很多人有一个愿望:将系统设计为能够被自由组合的组件.互联网(I ...

随机推荐

  1. Nginx应用案例分享:压力测试

    在运维工作中,压力测试是一项非常重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验. 但是,在压力测试中存在一个共性,那就是压力测试的 ...

  2. String 和 byte[]

    使用默认字符集合 Encodes this String into a sequence of bytes using the platform's default charset, storing ...

  3. JQuery要点(一)

    简介: jQuery Mobile 是用于创建移动 Web 应用的前端开发框架. jQuery Mobile 可以应用于智能手机与平板电脑. jQuery Mobile 使用 HTML5 & ...

  4. Line去年营收超5亿美元 远超竞争对手WhatsApp

    原文地址: http://news.cnblogs.com/n/206072/ 凭借着修改表情取悦国际用户的做法,日本移动消息应用 Line 在全球的用户总数已经超过 4 亿.Line.微信.What ...

  5. 检查dns服务器是否可用

    #%windir%\system32\WindowsPowerShell\v1.0\powershell.exe D:\PSScript\ERP_Production_Script\ERPRF_Upd ...

  6. 使用Filter防止浏览器缓存页面或请求结果

    仅仅须要两步: 1.定义一个Filter: /** * 防止浏览器缓存页面或请求结果 * @author XuJijun * */ public class NoCacheFilter impleme ...

  7. SQL Server 数据导入Mysql详细教程

  8. Java网页数据采集器[上篇-数据采集]【转载】

    开篇 作为全球运用最广泛的语言,Java 凭借它的高效性,可移植性(跨平台),代码的健壮性以及强大的可扩展性,深受广大应用程序开发者的喜爱. 作为一门强大的开发语言,正则表达式在其中的应用当然是必不可 ...

  9. Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理

    D. Chip 'n Dale Rescue Rangers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...

  10. Base64 图片转换工具

    以前在写asp的后台的时候,有一个上传功能是必须的,那时候进行的图片预览(未上传前)其实就是获取本地的图片路径来显示图片,但是随着HTML5的出现,可以把图片通过编码来实现预览. 在雅虎的36条速度优 ...