熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Nvelocity 實現動態加載 例子

2013-11-13 10:04:31  來源: .NET編程 

  因為工作需要使用Nvelocity 所以簡單的做了個小例子

  首先建立一個hmtl頁面 然後添加  替換的腳本如紅色的部分注意$後邊的字符不能事中文和獨立的數字

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head>
    <title></title>
</head>
<body>
&nbsp;$m $q
</body>
</html>
然後添加一個aspx 頁面 首先引用 Nvelocitydll

  然後

  using System;
using SystemCollectionsGeneric;
using NVelocity;
using NVelocityApp;
using NVelocityRuntime;
using NVelocityContext;
using CommonsCollections;
using SystemIO;
using SystemText;

  namespace loginDemo
{
    public partial class Nvelocity : SystemWebUIPage
    {
        protected void Page_Load(object sender EventArgs e)
        {

  }

  //這個方法用於頁面合並後打印到前台頁面
        void MergeTemplate()
        {
            //創建NVelocity引擎的實例對象
            VelocityEngine velocity = new VelocityEngine();
            //初始化該實例對象
            ExtendedProperties props = new ExtendedProperties();
            propsAddProperty(RuntimeConstantsRESOURCE_LOADER file);
            propsAddProperty(RuntimeConstantsFILE_RESOURCE_LOADER_PATH PathGetDirectoryName(RequestPhysicalPath));
            propsAddProperty(RuntimeConstantsINPUT_ENCODING gb);
            propsAddProperty(RuntimeConstantsOUTPUT_ENCODING gb);
            velocityInit(props);
            //從文件中讀取模板
            Template temp = velocityGetTemplate(template/);
            IContext context = new VelocityContext();
            contextPut(m );
            contextPut(q 李坤然);

  //合並模板
            StringWriter writer = new StringWriter();
            tempMerge(context writer);
            //輸入
            ResponseWrite(writerToString()Replace(\r\n <br/>));

  }

  //這個頁面用於再本地根據模板生成相應的 html文件cms中應該會用到
   void WriterTemplate

  {

  //創建NVelocity引擎的實例對象
        VelocityEngine velocity = new VelocityEngine();
        //初始化該實例對象
        ExtendedProperties props = new ExtendedProperties();
        propsAddProperty(RuntimeConstantsRESOURCE_LOADER file);
        propsAddProperty(RuntimeConstantsFILE_RESOURCE_LOADER_PATH PathGetDirectoryName(RequestPhysicalPath));
        propsAddProperty(RuntimeConstantsINPUT_ENCODING gb);
        propsAddProperty(RuntimeConstantsOUTPUT_ENCODING gb);
        velocityInit(props);
        //從文件中讀取模板
        Template temp = velocityGetTemplate(template/);
        IContext context = new VelocityContext();
        contextPut(m );
        contextPut(q 李坤然);

  //合並模板
        StringWriter writer = new StringWriter();

  tempMerge(context writer);
        //生成靜態頁
using (StreamWriter writer = new StreamWriter(ServerMapPath(/) + lfalseEncodingUTF))
        {
            writerWrite(writer);
            writerFlush();
            writerClose();
        }
}

  }
}


From:http://tw.wingwit.com/Article/program/net/201311/12480.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.