ASP生成静态页面的方法-ASP教程,ASP应用
(2009-06-18 14:00:29)
转载▼
标签: 杂谈
分类: 代码收集
asp2html with templet
我希望大家看到该标题就能让想象到他的功能:
1,with templet意思是,生成的页面架构将采用某个已设定的模板,在此之前我的一篇教程中介绍过,希望各位在看本教程之前对asp采用模板应熟悉下。(当然,不看也没有问题,本教程同样会提及精华部分的:)具体参考:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=474
2,asp2html。不要我再说asp转变成html的好处了吧,呵呵,其中最值得知道的就是:静态html页和动态页对服务器的需求承受能力小得多,同样,静态html搜索几率远比动态页面的多得多。
那么,我目前需要处理的技术问题就是:
1,怎么实现模板技术?(先参看下上篇文章吧)
2,怎么实现2html技术?
3,怎么让模板技术和2html技术结合?
一、先进行技术原理分析
1,模板技术参看 www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=474
2,2html技术又该怎么实现呢?怎么使得asp页面转变为html?一般都会想到fso组件,因为该组件能新建所有文件格式。
那么其整个运行过程是怎么样的呢?
a,提供信息输入页面进行信息收集;
b,接受信息值先保存数据库,再fso生成文件;
c,技术性完成任务,显示刚被创建的html文件的路径地址。
该技术的实现过程中有如下几个难点:
i,fso生成的文件是直接放在一个大目录下,还是独立放在某个每日更新的子目录中?可能表述不准确,这样理解吧:相信通过fso生成的文件随着时间的推移,文件会越来越多,管理也会越来越乱……通常你可能看到一些地址诸如 www.xxx.com/a/2004-5-20/[1**********]1.html 能分析得出应该是建立了当前日期的目录。这样,一天就是个目录的页面内容,查看管理也就显得比较合理。
ii,我在试图通过以上方法建立目录的时候,又发现了第二个问题。第一次通过fso建立以当前日期命名的目录,没有问题。当我有新的文件需要生成时,因为是同一个程式,所以,其又将会执行建立同样的目录。此时,fso组件会发现该路径已存在……卡壳-_-! 继续处理,在首行添加代码:
引用:
on error resume next
嘿嘿,达到自欺欺人、掩耳盗铃的效果。
当然规矩的用法是判断目录的有无
引用:
set fso = server.createobject("scripting.filesystemobject")
if (fso.folderexists(server.mappath(folder))) then
判断如果存在就不做处理
else
判断如果不存在则建立新目录
fso.createfolder(server.mappath(folder))
end if
%>
iii,目录是建立了,文件该怎么建立呢?主要也就是文件名的生成。当然这个就需要自己来写个函数,功能就是怎么生成文件名:)
引用:
function makefilename(fname)
fname = 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 & ".html"
end function
%>
引用函数则:
其实嘛,就是以年月日时分秒命名的文件。
iv,最后,生成的文件该怎么查看到?当然需要把生成文件的路径保存的数据库中,并且添加到相对应的记录集中了。当然,这在下面的数据库设计时会提及到。
3,模板技术和2html技术的结合:将模板中特别代码的值替换为从表单接受过来的值,完成模板功能;将最终替换过的所有模板代码生成html文件。需要注意的是:替换应能将输入数据的格式或支持ubb的代码完全改动。
二,再进行数据库设计
目前数据库的设计需要两个表:一个是存放模板数据的;一个是存放信息内容的。
1,建立新数据库asp2html.mdb
2,设计新数据库表c_moban
字段m_id(自动编号,主关键字);字段m_html(备注类型)。
并将下列完整的代码拷贝至m_html字段
引用:
cnbruce.com | asp2html test
$cntop{logcontent}lt;/td> | |
$cnleft{logcontent}lt;/td> | $cnright{logcontent}lt;/td> |
3,设计新数据库表c_news
字段c_id:自动编号,主关键字
字段c_title:文本类型,保存文章标题
字段c_content:备注类型,保存文章内容
字段c_filepath:文本类型,保持生成文件的路径地址
字段c_time:日期/时间类型,默认值:now()
三,页面需求设计
1,首先建立一个存放html页的目录
在文件同一目录下,建立目录newsfile,夹子内部主要存放生成的html页面,当然内部还会采用程式方式建立以日期命名的子目录,以方便浏览及管理。
2,功能函数页面lib.asp
引用:
生成文件名的函数
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
保持数据格式不变的函数
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), "
")
fstring = replace(fstring, chr(10), "
")
htmlencode = fstring
end function
%>
3,数据库连接页面conn.asp
完成数据库的字符串连接方法
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
其实非常简单:)就是表单嘛。注意action是跳转到addit.asp
引用:
5,处理数据功能显示页面addit.asp
首先是处理接受过来的数据,并将值写入数据库;接着将模板代码进行引用,并将其中特别代码转换为接受值,最终通过fso生成html页面。其中需要注意的更有,生成文件的路径地址保存至数据库表。
引用:
on error resume next
%>
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
fname = makefilename(now()) makefilename为自定义函数
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
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
%>
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{logcontent}quot;,now())
mb_code=replace(mb_code,"$cnleft{logcontent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{logcontent}quot;,c_content)
%>
set fso = server.createobject("scripting.filesystemobject")
fso.createfolder(server.mappath(folder))
set fout = fso.createtextfile(server.mappath(filepath))
fout.writeline mb_code
fout.close
%>
文章添加成功,浏览
6,显示数据库表记录,并做指向html页的链接:showit.asp
引用:
set rs = server.createobject ("adodb.recordset")
sql = "select * from c_news order by c_id desc"
rs.open sql,conn,1,1
%>
if rs.eof and rs.bof then
response.write ("暂时还没有文章,添加")
else
do until rs.eof
%>
target="a_blank"> | |
[>dell][>edit][add] |
rs.movenext
loop
end if
%>
rs.close
set rs = nothing
conn.close
set conn=nothing
%>
7,修改数据内容页change.asp
修改数据内容,同时也需要修改更新对应的html页面。修改其实就是重新生成文件,且文件名和之前相同,类似文件的覆盖。
引用:
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
%>
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{logcontent}quot;,now())
mb_code=replace(mb_code,"$cnleft{logcontent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{logcontent}quot;,c_content)
%>
set fso = server.createobject("scripting.filesystemobject")
set fout = fso.createtextfile(server.mappath(c_filepath))
fout.writeline mb_code
fout.close
%>
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
%>
8,删除记录页del.asp
同样!删除,除了删除数据库表中的记录,和其对应的html页面也需删除。代码如下:
引用:
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
%>
四,其他功能
模板管理页面:
不会每次都是打开数据库表进行增加或修改模板代码吧,所以,管理代码的页面程式不能少了,自己捣鼓下应该非常简单的。当然,之前管理员的登录认证程式就不在书中交代了:)更有,如果设计了多个模板,那么在发表信息的时候应添加模板选择单选框,同样在执行转换html时,sql选择的不同m_id了。
不管怎么说,先把这些技术自己调试感受下。多多操作,相信“读书千遍,其意自见”。
ASP生成静态页面的方法-ASP教程,ASP应用
(2009-06-18 14:00:29)
转载▼
标签: 杂谈
分类: 代码收集
asp2html with templet
我希望大家看到该标题就能让想象到他的功能:
1,with templet意思是,生成的页面架构将采用某个已设定的模板,在此之前我的一篇教程中介绍过,希望各位在看本教程之前对asp采用模板应熟悉下。(当然,不看也没有问题,本教程同样会提及精华部分的:)具体参考:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=474
2,asp2html。不要我再说asp转变成html的好处了吧,呵呵,其中最值得知道的就是:静态html页和动态页对服务器的需求承受能力小得多,同样,静态html搜索几率远比动态页面的多得多。
那么,我目前需要处理的技术问题就是:
1,怎么实现模板技术?(先参看下上篇文章吧)
2,怎么实现2html技术?
3,怎么让模板技术和2html技术结合?
一、先进行技术原理分析
1,模板技术参看 www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=474
2,2html技术又该怎么实现呢?怎么使得asp页面转变为html?一般都会想到fso组件,因为该组件能新建所有文件格式。
那么其整个运行过程是怎么样的呢?
a,提供信息输入页面进行信息收集;
b,接受信息值先保存数据库,再fso生成文件;
c,技术性完成任务,显示刚被创建的html文件的路径地址。
该技术的实现过程中有如下几个难点:
i,fso生成的文件是直接放在一个大目录下,还是独立放在某个每日更新的子目录中?可能表述不准确,这样理解吧:相信通过fso生成的文件随着时间的推移,文件会越来越多,管理也会越来越乱……通常你可能看到一些地址诸如 www.xxx.com/a/2004-5-20/[1**********]1.html 能分析得出应该是建立了当前日期的目录。这样,一天就是个目录的页面内容,查看管理也就显得比较合理。
ii,我在试图通过以上方法建立目录的时候,又发现了第二个问题。第一次通过fso建立以当前日期命名的目录,没有问题。当我有新的文件需要生成时,因为是同一个程式,所以,其又将会执行建立同样的目录。此时,fso组件会发现该路径已存在……卡壳-_-! 继续处理,在首行添加代码:
引用:
on error resume next
嘿嘿,达到自欺欺人、掩耳盗铃的效果。
当然规矩的用法是判断目录的有无
引用:
set fso = server.createobject("scripting.filesystemobject")
if (fso.folderexists(server.mappath(folder))) then
判断如果存在就不做处理
else
判断如果不存在则建立新目录
fso.createfolder(server.mappath(folder))
end if
%>
iii,目录是建立了,文件该怎么建立呢?主要也就是文件名的生成。当然这个就需要自己来写个函数,功能就是怎么生成文件名:)
引用:
function makefilename(fname)
fname = 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 & ".html"
end function
%>
引用函数则:
其实嘛,就是以年月日时分秒命名的文件。
iv,最后,生成的文件该怎么查看到?当然需要把生成文件的路径保存的数据库中,并且添加到相对应的记录集中了。当然,这在下面的数据库设计时会提及到。
3,模板技术和2html技术的结合:将模板中特别代码的值替换为从表单接受过来的值,完成模板功能;将最终替换过的所有模板代码生成html文件。需要注意的是:替换应能将输入数据的格式或支持ubb的代码完全改动。
二,再进行数据库设计
目前数据库的设计需要两个表:一个是存放模板数据的;一个是存放信息内容的。
1,建立新数据库asp2html.mdb
2,设计新数据库表c_moban
字段m_id(自动编号,主关键字);字段m_html(备注类型)。
并将下列完整的代码拷贝至m_html字段
引用:
cnbruce.com | asp2html test
$cntop{logcontent}lt;/td> | |
$cnleft{logcontent}lt;/td> | $cnright{logcontent}lt;/td> |
3,设计新数据库表c_news
字段c_id:自动编号,主关键字
字段c_title:文本类型,保存文章标题
字段c_content:备注类型,保存文章内容
字段c_filepath:文本类型,保持生成文件的路径地址
字段c_time:日期/时间类型,默认值:now()
三,页面需求设计
1,首先建立一个存放html页的目录
在文件同一目录下,建立目录newsfile,夹子内部主要存放生成的html页面,当然内部还会采用程式方式建立以日期命名的子目录,以方便浏览及管理。
2,功能函数页面lib.asp
引用:
生成文件名的函数
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
保持数据格式不变的函数
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), "
")
fstring = replace(fstring, chr(10), "
")
htmlencode = fstring
end function
%>
3,数据库连接页面conn.asp
完成数据库的字符串连接方法
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
其实非常简单:)就是表单嘛。注意action是跳转到addit.asp
引用:
5,处理数据功能显示页面addit.asp
首先是处理接受过来的数据,并将值写入数据库;接着将模板代码进行引用,并将其中特别代码转换为接受值,最终通过fso生成html页面。其中需要注意的更有,生成文件的路径地址保存至数据库表。
引用:
on error resume next
%>
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
fname = makefilename(now()) makefilename为自定义函数
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
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
%>
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{logcontent}quot;,now())
mb_code=replace(mb_code,"$cnleft{logcontent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{logcontent}quot;,c_content)
%>
set fso = server.createobject("scripting.filesystemobject")
fso.createfolder(server.mappath(folder))
set fout = fso.createtextfile(server.mappath(filepath))
fout.writeline mb_code
fout.close
%>
文章添加成功,浏览
6,显示数据库表记录,并做指向html页的链接:showit.asp
引用:
set rs = server.createobject ("adodb.recordset")
sql = "select * from c_news order by c_id desc"
rs.open sql,conn,1,1
%>
if rs.eof and rs.bof then
response.write ("暂时还没有文章,添加")
else
do until rs.eof
%>
target="a_blank"> | |
[>dell][>edit][add] |
rs.movenext
loop
end if
%>
rs.close
set rs = nothing
conn.close
set conn=nothing
%>
7,修改数据内容页change.asp
修改数据内容,同时也需要修改更新对应的html页面。修改其实就是重新生成文件,且文件名和之前相同,类似文件的覆盖。
引用:
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
%>
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{logcontent}quot;,now())
mb_code=replace(mb_code,"$cnleft{logcontent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{logcontent}quot;,c_content)
%>
set fso = server.createobject("scripting.filesystemobject")
set fout = fso.createtextfile(server.mappath(c_filepath))
fout.writeline mb_code
fout.close
%>
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
%>
8,删除记录页del.asp
同样!删除,除了删除数据库表中的记录,和其对应的html页面也需删除。代码如下:
引用:
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
%>
四,其他功能
模板管理页面:
不会每次都是打开数据库表进行增加或修改模板代码吧,所以,管理代码的页面程式不能少了,自己捣鼓下应该非常简单的。当然,之前管理员的登录认证程式就不在书中交代了:)更有,如果设计了多个模板,那么在发表信息的时候应添加模板选择单选框,同样在执行转换html时,sql选择的不同m_id了。
不管怎么说,先把这些技术自己调试感受下。多多操作,相信“读书千遍,其意自见”。