ASP生成静态全过程研究,从数据库到页面生成

数据库db1.mdb

表1:moban
字段:
id--自动编号
moban--备注字段--存储模版代码

模版代码(示例):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testmb</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>

表2:c_news
字段:
c_id 自动编号
c_title
c_content
c_filepath
c_time

-------------------------

文件1:add.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<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>
</body>
</html>
------------------------------------

文件2:showit.asp

<!--#include
file="conn.asp" -->
<!--#include file="lib.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
%> <table width="758" border="0" align="center" cellpadding="3"
cellspacing="1" bgcolor="#000000"> <tr> <td width="159"
align="right" bordercolor="#CCCCCC"
bgcolor="#CCCCCC"><%=rs("c_time")%></td> <td
width="591" bordercolor="#f3f3f3" bgcolor="#f3f3f3"><a
href=<%=rs("c_filepath")%>
target="a_blank"><%=rs("c_title")%></a></td>
</tr> <tr> <td valign="top" align="right"
bordercolor="#ececec" bgcolor="#ececec">[<a
href=del.asp?c_id=<%=rs("c_id")%>>Dell</a>][<a
href=change.asp?c_id=<%=rs("c_id")%>>Edit</a>][<a
href="add.html">Add</a>]</td> <td valign="top"
bordercolor="#FFFFFF"
bgcolor="#FFFFFF"><%=htmlencode(rs("c_content"))%></td>
</tr> </table><br>
<% rs.MoveNext
Loop
end if
%>
<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>
-----------------------------------

文件3:addit.asp

<%'容错处理
On Error Resume Next
%>

<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->

<%'接受传递值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>

<%'生成HTML文件名,建立文件夹,指定文件路径
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 id,moban from moban where id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("moban")
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="showit.asp">浏览</a>

文件4:change.asp

<!--#include file="conn.asp" -->
<!--#include 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
%>
<%'打开模板代码,并将其中特殊代码转变为接受值
sql1="select id,moban from moban where id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("moban")
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>
------------------------

文件5:conn.asp

<%
set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("db1.mdb")
conn.Open connstr
%>
------------------------

文件6;del.asp

<!--#include 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")%>
-----------------------

文件7: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 & ".html"
end function

'保持数据格式不变的函数
function HTMLEncode(fString)
fString = replace(fString, ">", "&gt;")
fString = replace(fString, "<", "&lt;")
fString = Replace(fString, CHR(32), "&nbsp;")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>
----------------------

文件8:mobanedit.asp

<!--#include file="conn.asp"-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<%
dim action,add,moban
action=request("action")
moban=request("moban")
if action="add" then
dim rs2,sql2
set rs2=server.CreateObject("adodb.recordset")
sql2="select * from moban"
rs2.open sql2,conn,3,3
rs2("moban")=moban
rs2.update
rs2.close
set rs2=nothing
response.Redirect("mobanedit.asp")
end if
%>
<%
set rs=server.CreateObject("adodb.recordset")
sql="select * from moban"
rs.open sql,conn,1,1
%>
<p>编辑模版:</p>
<form action="mobanedit.asp?action=add" method="post">
<p>
<textarea name="moban" cols="100" rows="30" id="moban"><%=rs("moban")%></textarea>
</p>
<p align="center">
<input type="submit" name="Submit" value="保存修改" />
</p>
</form>
<%
rs.close
set rs=nothing
%>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值