• NVelocity 操作类VelocityHelper


    原文连接:http://my.oschina.net/yangzhi/blog/6770

    代码

    using System;
    using System.Web;
    using System.IO;

    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
    using NVelocity.Runtime;
    using Commons.Collections;
    using System.Text;

    namespace FoodunTemplateAction
    {


    /// <summary>
    /// NVelocity模板工具类 VelocityHelper
    /// </summary>
    public class VelocityHelper
    {
    private VelocityEngine velocity = null;
    private IContext context = null;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="templatDir">模板文件夹路径</param>
    public VelocityHelper(string templatDir)
    {
    Init(templatDir);
    }

    /// <summary>
    /// 无参数构造函数
    /// </summary>
    public VelocityHelper() { }

    /// <summary>
    /// 初始话NVelocity模块
    /// </summary>
    public void Init(string templatDir)
    {
    //创建VelocityEngine实例对象
    velocity = new VelocityEngine();

    //使用设置初始化VelocityEngine
    ExtendedProperties props = new ExtendedProperties();
    props.AddProperty(RuntimeConstants.RESOURCE_LOADER,
    "file");
    props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(templatDir));
    //props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(HttpContext.Current.Request.PhysicalPath));
    props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
    props.AddProperty(RuntimeConstants.OUTPUT_ENCODING,
    "utf-8");

    //模板的缓存设置
    props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存
    props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒)

    velocity.Init(props);

    //为模板变量赋值
    context = new VelocityContext();
    }

    /// <summary>
    /// 给模板变量赋值
    /// </summary>
    /// <param name="key">模板变量</param>
    /// <param name="value">模板变量值</param>
    public void Put(string key, object value)
    {
    if (context == null)
    context
    = new VelocityContext();
    context.Put(key, value);
    }

    /// <summary>
    /// 显示模板
    /// </summary>
    /// <param name="templatFileName">模板文件名</param>
    public void Display(string templatFileName)
    {
    //从文件中读取模板
    Template template = velocity.GetTemplate(templatFileName);
    //合并模板
    StringWriter writer = new StringWriter();
    template.Merge(context, writer);
    //输出
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Write(writer.ToString());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 根据模板生成静态页面
    /// </summary>
    /// <param name="templatFileName"></param>
    /// <param name="htmlpath"></param>
    public void CreateHtml(string templatFileName, string htmlpath)
    {
    //从文件中读取模板
    Template template = velocity.GetTemplate(templatFileName);
    //合并模板
    StringWriter writer = new StringWriter();
    template.Merge(context, writer);
    using (StreamWriter write2 = new StreamWriter(HttpContext.Current.Server.MapPath(htmlpath), false, Encoding.UTF8, 200))
    {
    write2.Write(writer);
    write2.Flush();
    write2.Close();
    }

    }

    /// <summary>
    /// 根据模板生成静态页面
    /// </summary>
    /// <param name="templatFileName"></param>
    /// <param name="htmlpath"></param>
    public void CreateJS(string templatFileName, string htmlpath)
    {
    //从文件中读取模板
    Template template = velocity.GetTemplate(templatFileName);
    //合并模板
    StringWriter writer = new StringWriter();
    template.Merge(context, writer);
    using (StreamWriter write2 = new StreamWriter(HttpContext.Current.Server.MapPath(htmlpath), false, Encoding.UTF8, 200))
    {
    write2.Write(YZControl.Strings.Html2Js(YZControl.Strings.ZipHtml(writer.ToString())));
    write2.Flush();
    write2.Close();
    }

    }
    }

    }

    调用如下:

    VelocityHelper vh
    = new VelocityHelper();
    /// <summary>
    /// 显示页面
    /// </summary>
    public void ShowInfo()
    {
    vh.Init(
    "~/template/default");//模板路径
    PublicTemplate.GetHead(ref vh);
    vh.Put(
    "menu", 1);
    vh.Put(
    "minDate",DateTime.Now.ToShortDateString());
    vh.Put(
    "maxDate", DateTime.Now.AddMonths(3).ToShortDateString());

    if (CRequest.IsPost())//判断是什么请求
    {
    vh.Put(
    "post", true);
    TJ();
    }
    else
    {
    vh.Put(
    "post", false);
    }

    GetDCStore();

    vh.Display(
    "dc.html");
    }

    原创文字只代表本人某一时间内的观点或结论,本人不对涉及到的任何代码担保。转载请标明出处!

  • 相关阅读:
    Druid 使用 Kafka 将数据载入到 Kafka
    Druid 使用 Kafka 数据加载教程——下载和启动 Kafka
    Druid 集群方式部署 —— 启动服务
    Druid 集群方式部署 —— 端口调整
    Druid 集群方式部署 —— 配置调整
    Druid 集群方式部署 —— 配置 Zookeeper 连接
    Druid 集群方式部署 —— 元数据和深度存储
    Druid 集群方式部署 —— 从独立服务器部署上合并到集群的硬件配置
    Druid 集群方式部署 —— 选择硬件
    Druid 独立服务器方式部署文档
  • 原文地址:https://www.cnblogs.com/leleroyn/p/1871758.html
Copyright © 2020-2023  润新知