使用NVelocity0.5实现服务器端页面自动生成

使用NVelocity0.5实现服务器端页面自动生成

地球人都知道,静态HTML页面通常会比服务器端页面如asp、aspx页面要来的快,即使这些页面没有服务器端代码。 另外要命的是,这些页面在主流的搜索引擎能中最为吃香,和那些aspx还带几个尾巴参数的页面比起来,真是天上地下。 如果那天老板发现这个问题,叫你把辛辛苦苦实现的服务器端程序向静态HTML页面靠拢,你会做何感想? 有一种URL重写的方案可以实现对搜索引擎的欺骗,除了这种方法,自动生成静态HTML页面应该是最彻底的方法了。 言归正传,开始介绍如何实现吧 1. 引用Nvelocity0.5,记得是0.5哦,NVelocity0.4我试过好久,好像不行,好像和路径有关系。 2、引用一些需要的命名空间
using  NVelocity; using  NVelocity.App; using  NVelocity.Exception; using  NVelocity.Runtime; using  NVelocityTemplateEngine; using  NVelocityTemplateEngine.Interfaces;
3、初始化一些变量来使用
        INVelocityEngine fileEngine;         IDictionary context;
         /// <summary>         /// 初始化NVelocity模板引擎并加载程序的配置信息e         /// </summary>          protected   void  InitTemplateEngine()          {             context = new Hashtable();             string templateDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Articles");             fileEngine = NVelocityEngineFactory.CreateNVelocityFileEngine(templateDirectory, true);         }
4、页面生成代码
         public   override   void  Execute()          {             string message = string.Format("Create the Helper class file.");             log.Debug(message);             string sql = string.Format("select * from article ");             if (!isCreateAll)             {                 sql = string.Format("select * from article where generated =False ");             }             using (IDataReader reader = xConfig.ExecuteReader(sql))             {                 while (reader.Read())                 {                     PrepareClass(reader);                     OutputFile();                 }             }                          sql = "update article set generated =True ";             if (!isCreateAll)             {                 sql = "update article set generated =True  where generated =False ";             }             xConfig.ExecuteNonQuery(sql);         }          /// <summary>         /// Prepares the class content.         /// </summary>          private   void  PrepareClass(IDataReader reader)          {             FileNameOfOutput = string.Format("{0}#{1}", ((DateTime) reader["datetime"]).ToString("yyyy-MM-dd"), reader["id"].ToString());             context.Clear();             context.Add("id", reader["id"].ToString());             context.Add("category", reader["category"].ToString());             context.Add("title", reader["title"].ToString());             context.Add("content", reader["content"].ToString());             context.Add("datetime", reader["datetime"].ToString());         }
         /// <summary>         ///根据模板创建输出的文件         /// </summary>          public   virtual   void  OutputFile()          {             if (fileEngine != null)             {                 string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryOfOutput);                 string fileName = Path.Combine(filePath, fileNameOfOutput + fileExtension);                 DirectoryInfo dir = new DirectoryInfo(filePath);                 if (!dir.Exists)                 {                     dir.Create();                 }                                  log.Debug(string.Format("Class file output path:{0}", fileName));                 using (StreamWriter writer = new StreamWriter(fileName, false))                 {                     fileEngine.Process(context, writer, this.templateFile);                 }             }         }
5、界面层生成页面
             string  template  =   " page.htm " ;              try              {                 HelperClassAdapter helper = new HelperClassAdapter(template, false);                 helper.Execute();                 Response.Write("<script>alert('生成成功');</script>");             }              catch (Exception ex)              {                 Helper.ShowError(this, ex, false);                 return;             }
页面生成就可以了,具体做法自己琢磨就可以了。 页面模板文件
< HTML > < HEAD > < TITLE > $title </ TITLE > < META  http-equiv =Content-Type  content ="text/html; charset=UTF-8" > < META  content ="$title"  name =description > < META  content ="$title"  name =keywords >      </ HEAD >      < BODY >        < strong  class ="style3" > $title </ strong ></ h2 >        < div >  $content  </ div >        < hr  width ="98%" />        < div  align ="right" > $datetime </ div >      </ BODY > </ HTML >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值