小白一个,刚刚开始学,大神不要笑话。。。。。。
NVelocity是一个很容易上手的框架,从它开始学习,可以循序渐进
首先,创建空web应用程序,新建一般处理程序 Login2.ashx
然后,引入NVelocity,
c
创建Html 页面login.htm,实现登录框架
NVelocity语法是$开头
<html> <head></head> <body> <strong><font color='green'>登录</font></strong> <form action='Login2.ashx'> <input type='text' name='username' value='$username'/> <input type='password' name='password' value='$password' /> <input type='submit' value='登录'/> </form> <p>$msg</p> </body> </html>
编写Login2.ashx.cs页面,使用NVelocity处理请求
public class Login2 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string username = context.Request["username"]; string password = context.Request["password"]; if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password)) { VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("username", "");//设置参数,在模板中可以通过$data来引用 vltContext.Put("password", ""); vltContext.Put("msg", ""); Template vltTemplate = vltEngine.GetTemplate("login.htm"); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); context.Response.Write(html); } else { if (username == "admin" && password == "123") { context.Response.Write("登录成功"); } else { VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("username", username);//设置参数,在模板中可以通过$data来引用 vltContext.Put("password", password); vltContext.Put("msg", "用户名或者密码错误"); Template vltTemplate = vltEngine.GetTemplate("login.htm"); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); context.Response.Write(html); } } } public bool IsReusable { get { return false; } }
NVelocity的强大之处不仅仅这些,它还可以对 对象的属性进行赋值,看下面的例子
<html><head></head><body> $p.Name <p>我的老爹是 $p.Father.Name,他的年龄是$p.Father.Age </p> </body></html>
后台处理程序:
context.Response.ContentType = "text/html"; Person person = new Person(); person.Name = "张三"; person.Age = 30; Person dad = new Person(); dad.Name = "张三的爹"; dad.Age = 60; person.Father = dad; VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("p", person);//设置参数,在模板中可以通过$data来引用 Template vltTemplate = vltEngine.GetTemplate("test.htm"); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); context.Response.Write(html);
代码下载地址:http://download.csdn.net/detail/murongqingsi/9156661
NVelocity下载地址:http://download.csdn.net/detail/murongqingsi/9156623