模板页:
<!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="TestHandler.ashx" method="get">
name<input type="text" name="userName" /><br />
age<input type="text" name="userAge" />
<input type="checkbox" name="isVip" />U are vip?
<select name="sl">
<option value="bj">beijing</option>
<option value="nj">nanjing</option>
</select>
<input type="submit" />
</form>
</body>
</html>
应用处理处理程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
namespace Demo
{
/// <summary>
/// NewLoginHandler 的摘要说明
/// </summary>
public class NewLoginHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string name = context.Request["name"];
string password = context.Request["password"];
VelocityContext vlContext = new VelocityContext();//实例化一个VelocityContext对象
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(password))
{
VelocityEngine vlEngine = new VelocityEngine();
vlEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vlEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//括号内填写模板页所在文件夹
vlEngine.Init();
vlContext.Put("name", "");
vlContext.Put("password", "");
vlContext.Put("msg", "");
Template vlTemplate = vlEngine.GetTemplate("NewLogin.html");//模板页
System.IO.StringWriter vlWriter = new System.IO.StringWriter();
vlTemplate.Merge(vlContext, vlWriter);
String html = vlWriter.GetStringBuilder().ToString();
context.Response.Write(html);
}
else
{
if (name=="guanlin"&&password=="123")
{
vlContext.Put("name", name);
vlContext.Put("password", password);
vlContext.Put("msg", "success");
}
else
{
vlContext.Put("name", name);
vlContext.Put("password", password);
vlContext.Put("msg", "false");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}