日期:2014-05-17  浏览次数:20368 次

[技术散分]Asp.Net表示层方案(借助NVelocity模板引擎)
由于工作中常常需要使用模板配置文件来做动态的输出

而以前又采用的是Dom解析Xml的方式,导致如果模板中需要一个特殊处理,就得变动代码,以至于现在的不堪重负

不经意的时候,看到了NVelocity模板引擎,感觉很不错,语法很简单,很适合做UI,遂封装了下,现在分享给大家

如果您有任何的建议或者意见,都可以跟帖或者EMail我:NqIceCoffee@163.com

代码不难看懂,直接上代码了:

BaseHandler.cs

C# code
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; }
        }
    }
}


------解决方案--------------------
不太明白!
------解决方案--------------------
接分,感谢分享
------解决方案--------------------
不错呀,收藏!
------解决方案--------------------
没用过,我去网上找找,先接分
------解决方案--------------------
先接分 再慢慢看
------解决方案--------------------