使用NVelocity0.5实现服务器端页面自动生成
2008-06-29 12:28
344 查看
使用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>
相关文章推荐
- 使用NVelocity0.5实现服务器端页面自动生成
- 使用NVelocity0.5实现服务器端页面自动生成
- 使用NVelocity0.5实现服务器端页面自动生成
- 使用NVelocity实现服务器端页面自动生成
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法:服务器端和客户端数据类型的自动转换:复杂类型
- 使用scrollpagination实现页面底端自动加载无需翻页功能
- 使用Ext.UpdateManager实现页面任意部分自动刷新处理
- 使用jquery实现页面滚动到底部自动加载新的信息
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中实现高性能的ASP.NET管理页面自动生成
- 使用xmldom在服务器端生成静态html页面
- 页面启动自动使用jquery实现自动加载方法的三种形式
- 使用swagger实现在线api文档自动生成 在线测试api接口
- 使用 mkdocs 自动生成页面,文档中心。
- 使用java的html解析器jsoup和jQuery实现一个自动重复抓取任意网站页面指定元素的web应用
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 使用fileupload实现文件上传,并自动生成不同的文件夹
- JSP页面中实现使用taglib导入库时自动提示(JSTL 库安装)
- ASP.NET基础教程-Web 自定义控件的使用-根据属性值从数据库中提取数据并在页面上自动生成一个表格
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- 使用maven和eclipse自动生成的web工程中jsp页面c:out不生效的解决