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

本文介绍了一种将动态服务器端页面转换为静态HTML页面的方法,通过使用NVelocity模板引擎自动生成大量静态页面,以提高网站加载速度及搜索引擎优化效果。
地球人都知道,静态HTML页面通常会比服务器端页面如asp、aspx页面要来的快,即使这些页面没有服务器端代码。
另外要命的是,这些页面在主流的搜索引擎能中最为吃香,和那些aspx还带几个尾巴参数的页面比起来,真是天上地下。
如果那天老板发现这个问题,叫你把辛辛苦苦实现的服务器端程序向静态HTML页面靠拢,你会做何感想?
有一种URL重写的方案可以实现对搜索引擎的欺骗,除了这种方法,自动生成静态HTML页面应该是最彻底的方法了。
言归正传,开始介绍如何实现吧
1. 引用Nvelocity0.5,记得是0.5哦,NVelocity0.4我试过好久,好像不行,好像和路径有关系。
2、引用一些需要的命名空间
None.gifusing 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.gifContractedBlock.gif        /**//// <summary>
InBlock.gif        
/// 初始化NVelocity模板引擎并加载程序的配置信息e
ExpandedBlockEnd.gif        
/// </summary>

None.gif        protected void InitTemplateEngine()
ExpandedBlockStart.gifContractedBlock.gif        
dot.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.gifContractedBlock.gif        
dot.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.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sql 
= "update article set generated =True  where generated =False ";
ExpandedSubBlockEnd.gif            }

InBlock.gif            xConfig.ExecuteNonQuery(sql);
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
/// Prepares the class content.
ExpandedBlockEnd.gif        
/// </summary>

None.gif        private void PrepareClass(IDataReader reader)
ExpandedBlockStart.gifContractedBlock.gif        
dot.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.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
///根据模板创建输出的文件
ExpandedBlockEnd.gif        
/// </summary>

None.gif        public virtual void OutputFile()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (fileEngine != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif                
dot.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.gifContractedSubBlock.gif                
dot.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.gifContractedBlock.gif            
dot.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.gifContractedBlock.gif            
dot.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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值