使用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>
另外要命的是,这些页面在主流的搜索引擎能中最为吃香,和那些aspx还带几个尾巴参数的页面比起来,真是天上地下。
如果那天老板发现这个问题,叫你把辛辛苦苦实现的服务器端程序向静态HTML页面靠拢,你会做何感想?
有一种URL重写的方案可以实现对搜索引擎的欺骗,除了这种方法,自动生成静态HTML页面应该是最彻底的方法了。
言归正传,开始介绍如何实现吧
1. 引用Nvelocity0.5,记得是0.5哦,NVelocity0.4我试过好久,好像不行,好像和路径有关系。
2、引用一些需要的命名空间

















4、页面生成代码


































































5、界面层生成页面












页面生成就可以了,具体做法自己琢磨就可以了。
页面模板文件















转载请注明出处:撰写人:伍华聪 http://www.iqidi.com
标签:
代码生成工具
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)