当前位置: 首页 > news >正文

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

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


相关文章:

  • 回顾2016
  • secureCRT linux shell显示中文乱码 解决方法
  • json数据url传递到php后台
  • oracle的column格式化导致字段值显示为####的处理办法
  • 为docker配置固定ip
  • 爱上MVC3~将系统的路由设置抽象成对象吧
  • Vue2 SSR 的优化之旅
  • Linux~其实shell脚本也很简单
  • tomcat的maxThreads、acceptCount(最大线程数、最大排队数)
  • 【案例分享】电力设备生产数据的多层分组统计报表实现
  • 回顾2016,展望2017
  • SQL的一个排序的问题
  • RabbitMq应用二
  • Oracle Data Guard的配置
  • SolrJ 复杂查询 高亮显示
  • 〔开发系列〕一次关于小程序开发的深度总结
  • canvas 五子棋游戏
  • Django 博客开发教程 16 - 统计文章阅读量
  • HTTP中的ETag在移动客户端的应用
  • MySQL用户中的%到底包不包括localhost?
  • nodejs:开发并发布一个nodejs包
  • spring-boot List转Page
  • zookeeper系列(七)实战分布式命名服务
  • 安装python包到指定虚拟环境
  • 电商搜索引擎的架构设计和性能优化
  • 服务器从安装到部署全过程(二)
  • 技术:超级实用的电脑小技巧
  • 前端设计模式
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 入门到放弃node系列之Hello Word篇
  • 世界上最简单的无等待算法(getAndIncrement)
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 怎么将电脑中的声音录制成WAV格式
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (zt)最盛行的警世狂言(爆笑)
  • (八十八)VFL语言初步 - 实现布局
  • (附源码)计算机毕业设计ssm电影分享网站
  • (三)终结任务
  • (一)80c52学习之旅-起始篇
  • (转载)Google Chrome调试JS
  • .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • /boot 内存空间不够
  • @DataRedisTest测试redis从未如此丝滑
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(白虎组)
  • [1] 平面(Plane)图形的生成算法
  • [ACL2022] Text Smoothing: 一种在文本分类任务上的数据增强方法