zoukankan      html  css  js  c++  java
  • NVelocity模板引擎(生成静态页例子)

    这个是直接输入到页面上。如果我们不直接输出到页面上,而是把它写入到一个文件中呢?
    生成静态页--是的,这是让大家都心动的。 
    下面的代码是我第一个练习:
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
    using NVelocity.Runtime;
    /// <summary>
    /// 这个测试是基于NVelocity模板引擎实现的.
    /// </summary>

    public partial class NVelocity_模板引擎测试 System.Web.UI.Page
    {
       
    protected void Page_Load(object sender, EventArgs e)
       
    {
           
    //创建NVelocity引擎的实例对象
            VelocityEngine velocity = new VelocityEngine();
           
    //初始化该实例对象
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, 
    "file");
              //可换成:props.AddProperty("resouce.loader","file"),以下的同道理
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, 
    "gb2312");
            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, 
    "gb2312");
            velocity.Init(props); 
           
    //从文件中读取模板
            Template temp = velocity.GetTemplate("myTemplate.html");
            IContext context 
    = new VelocityContext();
            context.Put(
    "from""Sichuan");
            context.Put(
    "to""hainan");
            context.Put(
    "subject""welcome to nvelocity");
            context.Put(
    "name""McJeremy");
           
    //合并模板
            StringWriter writer = new StringWriter();
           
    //velocity.MergeTemplate(context, writer);
            temp.Merge(context, writer);
           
    //输入
            Response.Write(writer.ToString().Replace("\r\n""<br/>"));
        }
        
    }
    以下是生成静态页的练习:
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
    using NVelocity.Runtime;
    /// <summary>
    /// 这个测试是基于NVelocity模板引擎实现的.
    /// </summary>

    public partial class NVelocity_模板引擎测试 System.Web.UI.Page
    {
       
    protected void Page_Load(object sender, EventArgs e)
        
    {
           
    //创建NVelocity引擎的实例对象
            VelocityEngine velocity = new VelocityEngine();
           
    //初始化该实例对象
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, 
    "file");
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(Request.PhysicalPath));
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, 
    "gb2312");
            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, 
    "gb2312");
            velocity.Init(props); 
           
    //从文件中读取模板
            Template temp = velocity.GetTemplate("myTemplate.html");
            IContext context 
    = new VelocityContext();
            context.Put(
    "from""Sichuan");
            context.Put(
    "to""hainan");
            context.Put(
    "subject""welcome to nvelocity");
            context.Put(
    "name""McJeremy");
           
    //合并模板
            StringWriter writer = new StringWriter();
           
    //velocity.MergeTemplate(context, writer);
            temp.Merge(context, writer);
           
    //生成静态页
     using (StreamWriter writer2 = new StreamWriter(Server.MapPath("/"+ "test.html"), falseEncoding.UTF8, 200))
            
    {
                writer2.Write(writer);
                writer2.Flush();
                writer2.Close();
            }


        }
        
    }
  • 相关阅读:
    (数据科学学习手札33)基于Python的网络数据采集实战(1)
    (数据科学学习手札32)Python中re模块的详细介绍
    (数据科学学习手札31)基于Python的网络数据采集(初级篇)
    (数据科学学习手札30)朴素贝叶斯分类器的原理详解&Python与R实现
    (数据科学学习手札29)KNN分类的原理详解&Python与R实现
    (数据科学学习手册28)SQL server 2012中的查询语句汇总
    牛客3 F/G 牛牛的Link Power |线段树区间修改
    牛客2-H施魔法|线段树优化dp,维护区间最小值
    2020牛客寒假算法基础集训营1
    友情链接
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1453154.html
Copyright © 2011-2022 走看看