還是第一次在CSDN寫(xiě)文章,本人文采和理論知識(shí)有限,寫(xiě)得不正確的地方歡迎指正。其實(shí)網(wǎng)上已經(jīng)有很多ASP生成htm的文章了 ,有一種方法是ASP+XML的生成方法,雖然有一種好處就是不用程序?qū)懩0婢涂梢灾苯右迷瓉?lái)的要生成頁(yè)面源碼使用,但 本人進(jìn)行此方法測(cè)試時(shí),發(fā)現(xiàn)其穩(wěn)定性差和網(wǎng)速要求高(當(dāng)然不是在服務(wù)器上了)。特別是虛擬空間上經(jīng)常報(bào)錯(cuò),有可能在本 人在此方法上代碼處理不足的原因吧。長(zhǎng)話短說(shuō),這篇文章使用大家常用的另一種方法ASP+FSO,這里還應(yīng)用了框架就是為 了處理大量分頁(yè)時(shí)減少生成時(shí)間使用的,這種方法是針對(duì)一些頁(yè)面量較大的ASP文件。
這里我引用一個(gè)簡(jiǎn)單實(shí)例:(旅游電子商務(wù))全國(guó)各大城市酒店應(yīng)用靜態(tài)頁(yè)(htm)分頁(yè)顯示
1.應(yīng)用系統(tǒng)環(huán)境:win2000+ASP+MSSQL/ACCESS(數(shù)據(jù)庫(kù)基本沒(méi)有關(guān)系了通用的)+iis5.0
2.1個(gè)城市列表(CityHtml):包括定義靜態(tài)htm名稱共三個(gè)字段(城市ID(自動(dòng)編號(hào)),城市名稱(CityName例如北京),生成htm前綴名(HtmlStartName例如beijing))
3.1個(gè)全國(guó)酒店列表(Hotel):這里我只建立三個(gè)字段(酒店ID(自動(dòng)編號(hào)),城市名稱(City),酒店名稱(HotelName))方便來(lái)引用實(shí)例。
4.1個(gè)ASP頁(yè)面(ToHtm.asp)(生成htm使用)
5.1個(gè)循環(huán)框架頁(yè)面(IframeToHtm.asp),應(yīng)用框架批量生成htm
以下給出兩個(gè)頁(yè)面的源碼
循環(huán)框架進(jìn)行批量生成的頁(yè)面:IFrameToHtm.asp
<!--#include file="conn.asp"-->‘連接數(shù)據(jù)庫(kù) <% dim rs,sql,j set rs=Server.CreateObject("adodb.recordset") sql="select * from CityHtml"‘打開(kāi)全國(guó)城市列表 rs.open sql,conn,1,1 do until rs.eof‘循環(huán)各大城市%> <!--以下應(yīng)用框架打開(kāi)ToHtml生成頁(yè)面--> <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%>
生成程序頁(yè)面:ToHtm.asp 我在源碼大概寫(xiě)上注釋**
<!--#include file="conn.asp"-->‘?dāng)?shù)據(jù)連接文件 <% On Error Resume Next‘容錯(cuò)處理 Dim City‘定義取得要生成頁(yè)面的城市 City=Request.Querystring("City")‘獲取生成的城市酒店值從框架傳過(guò)來(lái)的在后面將介紹 HtmlStartName=Request.Querystring("HtmlStartName")‘獲得生成htm文件名前綴 Dim sql‘搜索字符串,這里我就直接打開(kāi)表不用搜索變量了,搜索條件按自己寫(xiě)就可以 sql="select * from Hotel where [City] = ‘" & City & "‘ " Dim oRs‘?dāng)?shù)據(jù)操作對(duì)象 Dim PageCounts‘實(shí)現(xiàn)分頁(yè)生成必須得知呀有多少頁(yè) Set oRs = Server.CreateObject("ADODB.Recordset") oRs.Open Sql,oConn,1,1‘找開(kāi)酒店等于City變量的表 oRs.pagesize=10‘十個(gè)記錄為一頁(yè) PageCounts=oRs.pagecount‘得出要生成多少個(gè)頁(yè)面,循環(huán)生成使用 Dim fs‘定義fso文件對(duì)象 Dim folders‘存放生成靜態(tài)頁(yè)的文件夾名稱 Dim Filestart‘定義生成htm文件前綴 Set fs=Server.CreateObject("Scripting.FileSystemObject") Dim i for i=1 to PageCounts‘開(kāi)始循環(huán)生成頁(yè)面,也就是分頁(yè)生成了 page=i oRs.absolutepage=i‘頁(yè)碼 rowcount=oRs.pagesize‘當(dāng)頁(yè)記錄數(shù) folders=server.mappath("CityHtml") if (fs.FolderExists(folders)) then‘判斷文件夾是否存在 else fs.CreateFolder(folders)‘不存在則創(chuàng)建CityHtml文件夾 end if if i=1 then Filestart=HtmlStartName‘如果為第一頁(yè)則定義文件名為傳值名.例如beijing則為beijing.htm else Filestart=HtmlStartName&i‘如果第二頁(yè)則為beijing+1例如有兩頁(yè)也就是i等于2則為 beijing2.htm如此類(lèi)推...(.htm后綴就在后面加上) end if Dim files‘定義生成文本文件名稱變量 Dim filez‘定義文件路徑名稱變量 files=Filestart&".txt"‘本文件名稱 filez=folders&"\"&"files‘文本文件路徑 ‘冊(cè)除文件 Dim checkfile‘檢查文本文件是否已經(jīng)存在,是則刪除 checkfile=server.mappath("CityHtml\"&Filestart&".htm")‘檢查htm文件是否已經(jīng)存在,是則刪除 if (fs.FileExists(checkfile)) then‘檢查htm文件是否已經(jīng)存在,是則刪除 Dim df‘定義文件對(duì)象*刪除文件使用* Set df=fs.GetFile(checkfile)‘定義要冊(cè)除的文件 df.delete‘冊(cè)除文件 end if‘判斷結(jié)束 Dim ts‘定義寫(xiě)入文件對(duì)象 set ts = fs.createtextfile(filez,true) ‘開(kāi)啟寫(xiě)入文件內(nèi)容**我在正文只簡(jiǎn)單寫(xiě)入酒店名稱和靜態(tài)數(shù)字分頁(yè)顯示** ts.write("<Html><Head><Title>生成"&City&"城市酒店</Title>"&vbcrlf)‘之后就是要生成的正文件內(nèi)容了跟使用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) ‘分頁(yè)輸出開(kāi)始 ‘?dāng)?shù)字分頁(yè)程序原理在這我就不多說(shuō)了,不懂的朋友可在網(wǎng)上搜索一下 Dim page‘當(dāng)前頁(yè) Dim Page2‘?dāng)?shù)字分頁(yè)變量 Dim s‘?dāng)?shù)字分頁(yè)變量 if page=1 then ts.write (" [首 頁(yè)] [前一頁(yè)] ") else ts.write (" <a href="&HtmlStartName&".htm"&" class=blue>[首 頁(yè)]</a> <a href="&HtmlStartName&Replace(page-1,1,"")&".htm"&" class=blue>前一頁(yè)</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 (" [后一頁(yè)] [尾 頁(yè)]") else ts.write (" <a href="&HtmlStartName&page+1&".htm"&" class=blue>[后一頁(yè)]</a> <a href="&HtmlStartName&ors.pagecount&".htm"&" class=blue>[尾 頁(yè)]</a>") end if ts.write("</TD></TR>") ‘分頁(yè)輸出結(jié)束 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‘當(dāng)頁(yè)記錄數(shù)-1 loop ts.write("</Table></body></html>"&vbcrlf) ts.close set ts=nothing ‘釋放對(duì)象 Dim EditFile‘定義改寫(xiě)文件變量 Set EditFile = fs.GetFile(filez)‘設(shè)置改寫(xiě)文件對(duì)象 EditFile.name= left(EditFile.name,len(EditFile.name)-4)&".htm" ‘改寫(xiě)文本文件成htm next‘循環(huán)生成結(jié)束(分頁(yè)生成) set EditFile=nothing ‘釋放對(duì)象 set fs=nothing‘釋放對(duì)象 if err.number<>0 then ‘處理生成錯(cuò)誤 Response.write(City&"更新時(shí)發(fā)生未知錯(cuò)誤<A href=ToHtml.asp?City="&City&"&HtmlName="&HtmlStartName&">重新更新</A>") else Response.Write(City&"酒店更新已完成 "&Now()) end if %>
|