二、再进行数据库设计
目前数据库的设计需要两个表:一个是存放模板数据的;一个是存放信息内容的。 1,建立新数据库asp2html.mdb
2、设计新数据库表c_moban
字段m_id(自动编号,主关键字);字段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{logcontent}lt;/td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft{logcontent}lt;/td>
<td width="74%" bgcolor="#f3f3f3">$cnright{logcontent}lt;/td>
</tr>
</table>
</body>
</html>
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), "<br>")
fstring = replace(fstring, chr(10), "<br>")
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
引用:
<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、处理数据功能显示页面addit.asp
首先是处理接受过来的数据,并将值写入数据库;接着将模板代码进行引用,并将其中特殊代码转换为接受值,最终通过fso生成html页面。其中需要注意的还有,生成文件的路径地址保存至数据库表。
引用:
<%'容错处理
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 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)
%>
<%'生成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>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器