使用NVelocity实现服务器端页面自动生成
2008-09-18 13:34
302 查看
地球人都知道,静态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、引用一些需要的命名空间
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实现服务器端页面自动生成
- 使用NVelocity0.5实现服务器端页面自动生成
- 使用mybatis-generator自动生成实体类,接口实现类和Mapper映射配置文件
- 使用NVelocity实现代码生成
- 使用jquery实现页面滚动到底部自动加载新的信息
- 使用jquery实现页面滚动到底部自动加载新的信息
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- SilverLight:使用MVVM实现View层在程序运行时自动生成控件并且取得其值
- 使用Myeclipse10.0自动生成搭建SSH框架(数据库表自动反向转换成Hibernate实体)实现用户登陆
- asp.net中倒计时自动跳转页面的实现方法(使用javascript)
- 使用PHP实现生成HTML静态页面
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- ASP.NET基础教程-Web 自定义控件的使用-根据属性值从数据库中提取数据并在页面上自动生成一个表格
- asp.net中实现上传文档到服务器端的指定文件夹,并且自动生成随机数更改文档的名字
- 在iframe中使用js代码实现自动设置栽入的页面的高度自动化
- 如何使用 RNN 模型实现文本自动生成
- 一个页面实现修改删除列表显示,ajax动态生成table(使用ajax,springmvc,jsp)
- 实现手机扫描二维码页面登录,类似web微信-第二篇,关于二维码的自动生成