62,051
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
using Commons.Collections;
namespace AspNet.App.UI
{
public abstract class BaseHandler : IHttpHandler
{
private static readonly VelocityEngine viewEngine = new VelocityEngine();
static BaseHandler()
{
//TODO:这里的硬编码可以改成配置文件的方式
ExtendedProperties extend = new ExtendedProperties();
extend.AddProperty(RuntimeConstants.COUNTER_NAME, "i");
extend.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
extend.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
extend.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
string appPath = AppDomain.CurrentDomain.BaseDirectory;
extend.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, appPath);
//模板的缓存设置
extend.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存
extend.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒)
viewEngine.Init(extend);
}
public HttpContext HttpContext
{
get;
private set;
}
public HttpRequest Request
{
get;
private set;
}
public HttpResponse Response
{
get;
private set;
}
private Hashtable templateData = new Hashtable();
public Hashtable TemplateData
{
get { return templateData; }
}
private string templateFile = string.Empty;
public string TemplateFile
{
get { return templateFile; }
set { templateFile = value; }
}
public void ProcessRequest(HttpContext context)
{
this.HttpContext = context;
this.Request = context.Request;
this.Response = context.Response;
//此方法主要进行数据的获取
Handler_Load(this, EventArgs.Empty);
//输出
IContext ctx = new VelocityContext(templateData);
viewEngine.MergeTemplate(TemplateFile, "utf-8", ctx, context.Response.Output);
}
//局部文件的呈现
protected string Merge(Hashtable data, string fileName)
{
IContext ctx = new VelocityContext(data);
StringBuilder sb = new StringBuilder(512);
StringWriter writer = new StringWriter(sb);
viewEngine.MergeTemplate(fileName, "utf-8", ctx, writer);
return sb.ToString();
}
public abstract void Handler_Load(object sender, EventArgs e);
public bool IsReusable
{
get { return false; }
}
}
}