还是第一次在csdn写文章,本人文采和理论知识有限,写得不正确的地方欢迎指正。其实网上已经有很多asp生成htm的文章了
,有一种方法是asp+xml的生成方法,虽然有一种好处就是不用程序写模版就可以直接引用原来的要生成页面源码使用,但
本人进行此方法测试时,发现其稳定性差和网速要求高(当然不是在服务器上了)。特别是虚拟空间上经常报错,有可能在本
人在此方法上代码处理不足的原因吧。长话短说,这篇文章使用大家常用的另一种方法asp+fso,这里还应用了框架就是为
了处理大量分页时减少生成时间使用的,这种方法是针对一些页面量较大的asp文件。
这里我引用一个简单实例:(旅游电子商务)全国各大城市酒店应用静态页(htm)分页显示
1.应用系统环境:win2000+asp+mssql/access(数据库基本没有关系了通用的)+iis5.0
2.1个城市列表(cityhtml):包括定义静态htm名称共三个字段(城市id(自动编号),城市名称(cityname例如北京),生成htm前缀名(htmlstartname例如beijing))
3.1个全国酒店列表(hotel):这里我只建立三个字段(酒店id(自动编号),城市名称(city),酒店名称(hotelname))方便来引用实例。
4.1个asp页面(tohtm.asp)(生成htm使用)
5.1个循环框架页面(iframetohtm.asp),应用框架批量生成htm
以下给出两个页面的源码
循环框架进行批量生成的页面:iframetohtm.asp
<!–#include file=”conn.asp”–>连接数据库
<%
dim rs,sql,j
set rs=server.createobject(“adodb.recordset”)
sql=”select * from cityhtml”打开全国城市列表
rs.open sql,conn,1,1
do until rs.eof循环各大城市%>
<!–以下应用框架打开tohtml生成页面–>
<iframe name=”loadrchtm<%=j%>” frameborder=0 width=100% height=30 scrolling=no src=”tohtml.asp?city=<%=cstr(rs(“city”))%>&htmlstartname=<%=rs(“htmlstart”)%>”></iframe>
<%rs.movenext
loop%>
生成程序页面:tohtm.asp 我在源码大概写上注释**
<!–#include file=”conn.asp”–>数据连接文件
<%
on error resume next容错处理
dim city定义取得要生成页面的城市
city=request.querystring(“city”)获取生成的城市酒店值从框架传过来的在后面将介绍
htmlstartname=request.querystring(“htmlstartname”)获得生成htm文件名前缀
dim sql搜索字符串,这里我就直接打开表不用搜索变量了,搜索条件按自己写就可以
sql=”select * from hotel where [city] = ” & city & ” “
dim ors数据操作对象
dim pagecounts实现分页生成必须得知呀有多少页
set ors = server.createobject(“adodb.recordset”)
ors.open sql,oconn,1,1找开酒店等于city变量的表
ors.pagesize=10十个记录为一页
pagecounts=ors.pagecount得出要生成多少个页面,循环生成使用
dim fs定义fso文件对象
dim folders存放生成静态页的文件夹名称
dim filestart定义生成htm文件前缀
set fs=server.createobject(“scripting.filesystemobject”)
dim i
for i=1 to pagecounts开始循环生成页面,也就是分页生成了
page=i
ors.absolutepage=i页码
rowcount=ors.pagesize当页记录数
folders=server.mappath(“cityhtml”)
if (fs.folderexists(folders)) then判断文件夹是否存在
else
fs.createfolder(folders)不存在则创建cityhtml文件夹
end if
if i=1 then
filestart=htmlstartname如果为第一页则定义文件名为传值名.例如beijing则为beijing.htm
else
filestart=htmlstartname&i如果第二页则为beijing+1例如有两页也就是i等于2则为 beijing2.htm如此类推…(.htm后缀就在后面加上)
end if
dim files定义生成文本文件名称变量
dim filez定义文件路径名称变量
files=filestart&”.txt”本文件名称
filez=folders&”\”&”files文本文件路径
册除文件
dim checkfile检查文本文件是否已经存在,是则删除
checkfile=server.mappath(“cityhtml\”&filestart&”.htm”)检查htm文件是否已经存在,是则删除
if (fs.fileexists(checkfile)) then检查htm文件是否已经存在,是则删除
dim df定义文件对象*删除文件使用*
set df=fs.getfile(checkfile)定义要册除的文件
df.delete册除文件
end if判断结束
dim ts定义写入文件对象
set ts = fs.createtextfile(filez,true) 开启写入文件内容**我在正文只简单写入酒店名称和静态数字分页显示**
ts.write(“<html><head><title>生成”&city&”城市酒店</title>”&vbcrlf)之后就是要生成的正文件内容了跟使用response.write
ts.write(“<meta http-equiv=content-type content=text/html; charset=gb2312>”&vbcrlf)
ts.write(“<meta name=keywords content=”&city&”酒店>”&vbcrlf)
ts.write(“<link href=/style/style.css rel=stylesheet type=text/css></head><body topmargin=0>”&vbcrlf)
ts.write(“<table width=760 cellspacing=0 cellpadding=0 align=center>”&vbcrlf&_
“<tr><td width=100%>”&vbcrlf)
分页输出开始
数字分页程序原理在这我就不多说了,不懂的朋友可在网上搜索一下
dim page当前页
dim page2数字分页变量
dim s数字分页变量
if page=1 then
ts.write (” [首 页] [前一页] “)
else
ts.write (” <a href=”&htmlstartname&”.htm”&” class=blue>[首 页]</a> <a href=”&htmlstartname&replace(page-1,1,””)&”.htm”&” class=blue>前一页</a> “)
end if
page2=(page-(page mod 10))/10
if page2<1 then page2=0
for s=page2*10-1 to page2*10+10
if s>0 then
if s=cint(page) then
ts.write (” <font color=#000000>[“& s & “]</font>”)
else
if s=1 then
ts.write (” <a href=”&htmlstartname&replace(s,1,””)&”.htm”&” class=blue>[“& s &”]</a>”)
else
ts.write (” <a href=”&htmlstartname&s&”.htm”&” class=blue>[“& s &”]</a>”)
end if
end if
if s=ors.pagecount then
exit for
end if
end if
next
if cint(page)=ors.pagecount then
ts.write (” [后一页] [尾 页]”)
else
ts.write (” <a href=”&htmlstartname&page+1&”.htm”&” class=blue>[后一页]</a> <a href=”&htmlstartname&ors.pagecount&”.htm”&” class=blue>[尾 页]</a>”)
end if
ts.write(“</td></tr>”)
分页输出结束
do while not ors.eof and rowcount>0 输出酒店名称
ts.write(“<tr><td width=100%>”&ors.fields(“chinese_name”)&”</td></tr>”&vbcrlf)
ors.movenext
rowcount=rowcount-1当页记录数-1 loop
ts.write(“</table></body></html>”&vbcrlf)
ts.close
set ts=nothing 释放对象
dim editfile定义改写文件变量
set editfile = fs.getfile(filez)设置改写文件对象
editfile.name= left(editfile.name,len(editfile.name)-4)&”.htm” 改写文本文件成htm
next循环生成结束(分页生成)
set editfile=nothing 释放对象
set fs=nothing释放对象
if err.number<>0 then 处理生成错误
response.write(city&”更新时发生未知错误<a href=tohtml.asp?city=”&city&”&htmlname=”&htmlstartname&”>重新更新</a>”)
else
response.write(city&”酒店更新已完成 “&now())
end if
%>
