如何使得ASP頁面轉(zhuǎn)變?yōu)镠TML?一般都會想到FSO組件,因?yàn)樵摻M件能新建 任何文件格式。
那么其整個運(yùn)行過程是怎么樣的呢?
a,提供信息輸入頁面進(jìn)行信息收集;
b,接受信息值先保存數(shù)據(jù)庫,再FSO生成文件;
c,技術(shù)性完成任務(wù),顯示剛被創(chuàng)建的HTML文件的路徑地址。
該技術(shù)的實(shí)現(xiàn)過程中有如下幾個難點(diǎn):
i,F(xiàn)SO生成的文件是直接放在一個大文件夾下,還是單獨(dú)放在某個每日更新的子文件夾中?可能表述不準(zhǔn)
確,這樣理解吧:相信通過FSO生成的文件隨著時間的推移,文件會越來越多,管理也會越來越亂……通
常你可能看到一些地址諸如 http://www.xxx.com/a/2004-5-20/200405201111.html ; 可以分析得出應(yīng)該
是建立了當(dāng)前日期的文件夾。這樣,一天就是一個文件夾的頁面內(nèi)容,查看管理也就顯得比較合理。
ii,我在試圖通過以上方法建立文件夾的時候,又發(fā)現(xiàn)了第二個問題。第一次通過FSO建立以當(dāng)前日期命
名的文件夾,沒有問題。當(dāng)我有新的文件需要生成時,因?yàn)槭峭粋程序,所以,其又將會執(zhí)行建立同樣
的文件夾。此時,F(xiàn)SO組件會發(fā)現(xiàn)該路徑已存在……卡殼-_-! 繼續(xù)處理,在首行添加代碼:
On Error Resume Next
嘿嘿,達(dá)到自欺欺人、掩耳盜鈴的效果。
iii,文件夾是建立了,文件該如何建立呢?主要也就是文件名的生成。當(dāng)然這個就需要自己來寫個函數(shù)
,功能就是如何生成文件名:)
<%
function makefilename(fname)
fname = fname ’前fname為變量,后fname為函數(shù)參數(shù)引用
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename = fname & ".html"
end function
%>
引用函數(shù)則:
<%fname = makefilename(now())%>
其實(shí)嘛,就是以年月日時分秒命名的文件。
iv,最后,生成的文件該如何查看到?當(dāng)然需要把生成文件的路徑保存的數(shù)據(jù)庫中,并且添加到相對應(yīng)的
記錄集中了。當(dāng)然,這在下面的數(shù)據(jù)庫設(shè)計(jì)時會提及到。
3,模板技術(shù)和2HTML技術(shù)的結(jié)合:將模板中特殊代碼的值替換為從表單接受過來的值,完成模板功能;將
最終替換過的所有模板代碼生成HTML文件。需要注意的是:替換應(yīng)能將輸入數(shù)據(jù)的格式或者支持UBB的代
碼徹底改變。
二,再進(jìn)行數(shù)據(jù)庫設(shè)計(jì)
目前數(shù)據(jù)庫的設(shè)計(jì)需要兩個表:一個是存放模板數(shù)據(jù)的;一個是存放信息內(nèi)容的。
1,建立新數(shù)據(jù)庫asp2html.mdb
2,設(shè)計(jì)新數(shù)據(jù)庫表c_moban
字段m_id(自動編號,主關(guān)鍵字);字段m_html(備注類型)。
并將下列完整的代碼拷貝至m_html字段
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=hz">
<title>Cnbruce.Com | ASP2HTML TEST</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$cntop$</td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft$</td>
<td width="74%" bgcolor="#f3f3f3">$cnright$</td>
</tr>
</table>
</body>
</html>
3,設(shè)計(jì)新數(shù)據(jù)庫表c_news
字段c_id:自動編號,主關(guān)鍵字
字段c_title:文本類型,保存文章標(biāo)題
字段c_content:備注類型,保存文章內(nèi)容
字段c_filepath:文本類型,保持生成文件的路徑地址
字段c_time:日期/時間類型,默認(rèn)值:Now()
三,頁面需求設(shè)計(jì)
1,首先建立一個存放HTML頁的文件夾
在文件同一目錄下,建立文件夾newsfile,夾子內(nèi)部主要存放生成的HTML頁面,當(dāng)然內(nèi)部還會采用程序方
式建立以日期命名的子文件夾,以方便瀏覽以及管理。
2,功能函數(shù)頁面lib.asp
<%
’生成文件名的函數(shù)
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ".shtml"
end function
’保持?jǐn)?shù)據(jù)格式不變的函數(shù)
function HTMLEncode(fString)
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>
3,數(shù)據(jù)庫連接頁面conn.asp
完成數(shù)據(jù)庫的字符串連接方法
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb")
conn.Open connstr
%>
4,信息輸入頁面add.html
其實(shí)很簡單:)就是表單嘛。注意action是跳轉(zhuǎn)到addit.asp
<form action="addit.asp" method="post">
Title:<input type="text" name="c_title"><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"></textarea><br>
<input type="submit" value="Add">
<input type="reset" value="Reset">
</form>
5,處理數(shù)據(jù)功能顯示頁面addit.asp
首先是處理接受過來的數(shù)據(jù),并將值寫入數(shù)據(jù)庫;接著將模板代碼進(jìn)行引用,并將其中特殊代碼轉(zhuǎn)換為接
受值,最終通過FSO生成HTML頁面。其中需要注意的還有,生成文件的路徑地址保存至數(shù)據(jù)庫表。
<%’容錯處理
On Error Resume Next
%>
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%’接受傳遞值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
<%’生成HTML文件名,建立文件夾,指定文件路徑
fname = makefilename(now()) ’makefilename為自定義函數(shù)
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
<%’將接受值及路徑保持至數(shù)據(jù)庫表
sql = "Select * from c_news"
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,3,2
rs.addnew
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_filepath")=filepath
rs.update
rs.close
Set rs = Nothing
%>
<%’打開模板代碼,并將其中特殊代碼轉(zhuǎn)變?yōu)榻邮苤?nbsp;
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop$",now())
mb_code=replace(mb_code,"$cnleft$",c_title)
mb_code=replace(mb_code,"$cnright$",c_content)
%>
<%’生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(Server.MapPath(folder))
Set fout = fso.CreateTextFile(Server.MapPath(filepath))
fout.WriteLine mb_code
fout.close
%>
文章添加成功,<a href="/blog/showit.asp">瀏覽</a>
6,顯示數(shù)據(jù)庫表記錄,并做指向HTML頁的鏈接:showit.asp
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%’打開模板代碼,并將其中特殊代碼轉(zhuǎn)變?yōu)榻邮苤?nbsp;
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop$",now())
mb_code=replace(mb_code,"$cnleft$",c_title)
mb_code=replace(mb_code,"$cnright$",c_content)
%>
<%’生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
7,修改數(shù)據(jù)內(nèi)容頁chang.asp
修改數(shù)據(jù)內(nèi)容,同時也需要修改更新對應(yīng)的HTML頁面。修改其實(shí)就是重新生成文件,且文件名和之前一樣
,類似文件的覆蓋。
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%’打開模板代碼,并將其中特殊代碼轉(zhuǎn)變?yōu)榻邮苤?nbsp;
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop$",now())
mb_code=replace(mb_code,"$cnleft$",c_title)
mb_code=replace(mb_code,"$cnright$",c_content)
%>
<%’生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
8,刪除記錄頁del.asp
同樣!刪除,除了刪除數(shù)據(jù)庫表中的記錄,與其對應(yīng)的HTML頁面也需刪除。代碼如下:
<!--#i nclude file="conn.asp" -->
<%
c_id = request.querystring("c_id")
sql = "Select * from c_news where c_id="&c_id
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,2,3
filepath=rs("c_filepath")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(Server.mappath(filepath))
Set fso = nothing
rs.delete
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
<%response.redirect("showit.asp")%>
四,其它功能
模板管理頁面:
不會每次都是打開數(shù)據(jù)庫表進(jìn)行增加或者修改模板代碼吧,所以,管理代碼的頁面程序不能少了,自己搗
鼓下應(yīng)該很簡單的。當(dāng)然,之前管理員的登錄認(rèn)證程序就不在書中交代了:)還有,如果設(shè)計(jì)了多個模板
,那么在發(fā)表信息的時候應(yīng)添加模板選擇單選框,同樣在執(zhí)行轉(zhuǎn)換HTML時,SQL選擇的不同m_id了。
不管怎么說,先把這些技術(shù)自己調(diào)試感受下。多多操作,相信“讀書千遍,其意自見”。
-----------------------------------
【收集二】、
首先這項(xiàng)技術(shù)的優(yōu)點(diǎn)是:(1)減輕數(shù)據(jù)庫的壓力;(2)把數(shù)據(jù)庫和頁面隔離開來
然后給大家講一下實(shí)現(xiàn)原理..
ASP生成HTML其實(shí)是使用服務(wù)器的FSO內(nèi)置對象..
其定義方法為
set fs=createobject("scripting.filesystemobject") \\’設(shè)置FSO對象
在建立FSO對象后就可以對服務(wù)器進(jìn)行文件及文件夾管理操作...
所以在服務(wù)器新建一個網(wǎng)頁文件也是很輕松的事...
sub SaveText(FileName,Data) \\’這是一個用于寫文本文件的過程
dim fs,ts,path \\’定義變量
set fs=createobject("scripting.filesystemobject") \\’設(shè)置FSO對象
if instr(filename,":\\\\")<>0 then \\’判斷是不是絕對路徑
path=filename
else
path=server.MapPath(FileName)
end if
set ts=fs.createtextfile(path,true) \\’創(chuàng)建文件對象
ts.writeline(data) \\’寫數(shù)據(jù)
ts.close \\’關(guān)閉對象
set ts=nothing
set fs=nothing
end sub
這是一個建立所在類型文件的子程序..
對重要語句都給了一定的注釋...
功能就是向服務(wù)器的指定路徑創(chuàng)建一個文件并將數(shù)據(jù)寫進(jìn)去..
其調(diào)用方式為:
savetext "D:\\\\chris.html","chris"
大家可以在自己的電腦上試一下..
第一個參數(shù)為Chris.html
文件內(nèi)容為chris
了解了創(chuàng)建文件的原理之后就可以對網(wǎng)站進(jìn)行批量網(wǎng)頁生成..
但是在生成以前我們必須得為生成的頁面制作一個模板...
下面我舉個例子..
Mode.asp
------------------
<body onLoad="window.focus();">
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0" class="table">
<tr>
<td><br>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td style="LEFT: 0px; WIDTH: 600 xp; WORD-WRAP: break-word"><p><font size="2"><%=rs
("bigclass")%> -> <%=rs("smallclass")%> -> <font color="#FF9B9B"><%=rs("title")%
></font></font></p>
<p><font size="2"><%response.Write(ubbcode(rs("content")))%></font></p>
<p> </p>
<p align="right"><font size="2">摘自:<%=rs("path")%> </font></p></td>
</tr>
</table></td>
</tr>
<tr>
<td><div align="center"><font size="2"><br>
發(fā)布時間:<font color="#FF9B9B"><%=rs("time")%></font> 瀏覽次數(shù):<font
color="#FF9B9B"><%=rs("browse")%></font></font></div></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
這是一個模板文件....
下面要做的操作就是制作一個生成頁面...
這個生成頁面要做的工作是將Mode.asp的文件內(nèi)容讀出.然后將其需要動態(tài)替換的地方替換成需要的內(nèi)
容..
set fso=server.CreateObject("scripting.filesystemobject") \\’創(chuàng)建一個FSO對象
set myfile=fso.getfile(filepath) \\’設(shè)置一個文件對象..filepath就是這個模板文件的名稱
set ts=myfile.openastextstream \\’設(shè)置一個文本對象..并打開這個對象...
if not ts.atendofstream then content=changecontent(ts.readline)
do while not ts.atendofstream
content=content+vbcrlf
content=content+changecontent(ts.readline)
loop
將模板文件的內(nèi)容賦值到content變量里...
再對其模板內(nèi)容處理后生成新的網(wǎng)頁文件...
例如
Replace(content,"<%=rs("title")%"&">",title)
將模板文件的<%=Rs("title")%>替換為文章標(biāo)題...
其他的Content,Time,Browse以此類推...
Content進(jìn)行處理后就是一個完整的網(wǎng)頁文件..
將其輸出就完成了網(wǎng)頁的生成..
sub SaveText(FileName,Data) \\’這是一個用于寫文本文件的過程
dim fs,ts,path \\’定義變量
set fs=createobject("scripting.filesystemobject") \\’設(shè)置FSO對象
if instr(filename,":\\\\")<>0 then \\’判斷是不是絕對路徑
path=filename
else
path=server.MapPath(FileName)
end if
set ts=fs.createtextfile(path,true) \\’創(chuàng)建文件對象
ts.writeline(data) \\’寫數(shù)據(jù)
ts.close \\’關(guān)閉對象
set ts=nothing
set fs=nothing
end sub
savetext CreateFileName,Content \\’調(diào)用寫文件子程序
使用模板自動生成原理基本上就是這樣...
主要問題在于大家使用過程中的一些細(xì)節(jié)問題的注意...
一會兒我會讓大家看一個比較成熟的FSO的展示程序...
set myfile=fso.getfile(uta(filepath))
----------------------
在FSO對象的基礎(chǔ)上建立一個文件對象...
myfile=你指定的那個文件..
if not ts.atendofstream then content=changecontent(ts.readline) \\’判斷文件是否為空.如果不為
空才進(jìn)行賦值操作
do while not ts.atendofstream \\’AtEndOfStream判斷是不是文件的末尾
content=content+vbcrlf
content=content+changecontent(ts.readline)
loop
FSO對象的屬性和方法比較多...如果大家感興趣可以參考一下VBS參考手冊..
我在這里只是起一個引導(dǎo)作用...給大家講解一下成生網(wǎng)頁文件..
content=content+vbcrlf
content=content+changecontent(ts.readline)
是做什么的
------------------------------------
把文件按行讀出來...
AtEndOfStream判斷是不是文件的末尾 剛才Chris已經(jīng)講了
do while not ts.atendofstream \\’AtEndOfStream判斷是不是文件的末尾 content=content+vbcrlf
content=content+changecontent(ts.readline)
loop
的意思就是說,把文件中的一行一行讀出來
只是一個函數(shù)..
function uta(val)
uta=replace(val,"*","&")
end function
filepath是在哪里取的?
filepath就是你那個文件所在地呀
2005-07-09 15:36:19 偸米飯(44018790)
Chris
changecontent這個函數(shù)有什么用?
changecontent是一個函數(shù),可能是替換里面的一些字符
那些都是一些Replace的替換操作...
你們根據(jù)自己需要進(jìn)行處理...
不一定必須...
整體原理就是利用FSO的創(chuàng)建文件方法在服務(wù)器創(chuàng)建一個網(wǎng)頁文件..
然后把代碼寫到創(chuàng)建的文件里...就是這樣.. |