NVelocity封装类VelocityHelper及用法
来源:互联网 发布:笨方法学python 编辑:程序博客网 时间:2024/06/03 16:30
代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using NVelocity;
- using NVelocity.App;
- using NVelocity.Context;
- using NVelocity.Runtime;
- using Commons.Collections;
- using System.IO;
- ///<summary>
- ///VelocityHelper 的摘要说明
- ///</summary>
- publicclassVelocityHelper
- {
- privateVelocityEngine ve = null;
- privateIContext context = null;
- public VelocityHelper(string templateDirectory)
- {
- Init(templateDirectory);
- }
- //初始化
- privatevoid Init(string templateDirectory)
- {
- ve = newVelocityEngine();
- ExtendedProperties ep = newExtendedProperties();
- if (templateDirectory != null)
- {
- ep.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
- ep.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(templateDirectory));
- }
- ep.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
- ep.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
- ve.Init(ep);
- context = newVelocityContext();
- }
- public VelocityHelper() : this(null)
- {
- }
- //设置参数
- publicvoid Put(string key, object value)
- {
- if (context == null)
- context = newVelocityContext();
- context.Put(key, value);
- }
- //显示模板
- publicvoid Display(string templateFileName)
- {
- Template template = ve.GetTemplate(templateFileName);
- StringWriter writer = newStringWriter();
- template.Merge(context, writer);
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.Write(writer.ToString());
- HttpContext.Current.Response.Flush();
- HttpContext.Current.Response.End();
- }
- //返回模板内容
- publicstring ToString(string templateFileName)
- {
- Template template = ve.GetTemplate(templateFileName);
- StringWriter writer = newStringWriter();
- template.Merge(context, writer);
- return writer.ToString();
- }
- }
使用这个类后原有代码就非常简单了:
- protectedvoid Button1_Click(object sender, EventArgs e)
- {
- VelocityHelper vh = newVelocityHelper("");
- vh.Put("name", "哈哈");
- Label1.Text = vh.ToString("demo1.htm");
- }
- protectedvoid Button2_Click(object sender, EventArgs e)
- {
- VelocityHelper vh = newVelocityHelper("");
- vh.Put("request", Request);
- vh.Put("context", HttpContext.Current);
- vh.Display("sample.vm");
- }
哈哈!示例下载: NVelocity.rar
- NVelocity封装类VelocityHelper及用法
- NVelocity 操作类VelocityHelper
- NVelocity 操作类VelocityHelper
- Net下模板引擎NVelocity的封装类――VelocityHelper
- Net下模板引擎NVelocity的封装类――VelocityHelper
- Velocity简单语法及VelocityHelper封装
- NVelocity用法
- NVelocity的简单用法
- nvelocity的单例用法
- Nvelocity及MonoRail比较研究
- Nvelocity及MonoRail比较研究
- NVelocity
- NVelocity
- NVelocity
- Nvelocity
- NVelocity模板引擎用法之模板中直接调用类的方法
- NVelocity用法 Net版(完全整理)
- 每个人应该知道的NVelocity用法
- debugPrint-1.1
- 新的开始
- 学习累了休息一下——————看完不笑你厉害
- ASP.NET中的模板引擎Nvelocity简单示例
- NVelocity语法
- NVelocity封装类VelocityHelper及用法
- 基于NVeloCity的静态页面生成示例(NVeloCity+LinQ)
- 住建部称第三套房不放贷政策或将取消!
- 文档
- 学习进程
- 感谢腾讯封了我的号
- HELLO
- 重定向无法设置页面编码?
- 希望朋友越来越多!!