分享
 
 
 

ASP模拟MVC模式编程

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

大家都知道JAVA里最流行的是MVC模型的编程方式,如果你不知道MVC的概念,可以去网上搜索下,应该会马上找到N多资料。PHP5推出之后,也开始使用MVC模式开发,看来MVC确实有很大的吸引力!

我在平时开发ASP项目的时候,也借鉴了JAVA的这个开发模式,虽然实现的不是很好,但也使代码结构清晰了不少,大大方便了美工修改程序!

下面是我的代码,具体说明看注释。

程序主要实现是后台管理员的操作:添加,删除,修改

分两个程序,adminusers_class.asp,adminusers_gl.asp

相当于JAVA力一个servlet,一个bean!所以没有完全的实现MVC,只是模拟了一下!

adminusers_class.asp代码如下:

<%

rem *****************通用网站管理系统***********************************

rem 页面功能:网站后台管理员管理类

rem @author 蔡晓亮

rem @version 1.0

rem @date 2005-2-22

%>

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

<%

'管理员管理类

Class AdminUsersGl

Dim ASO

Private adminuser_name

Private adminuser_password

Private admingroup_id

Private adminuser_isshow

Private adminuser_selfconfig

Private adminuser_realname

'构造函数开始

Private Sub Class_Initialize

Set ASO=Server.CreateObject("Adodb.RecordSet")

End Sub

'析构函数开始

Private Sub Class_Terminate

Err.Clear

Set ASO=Nothing

End Sub

'添加管理员

Public Function AdminUsers_Add(adminuser_name,adminuser_password,admingroup_id,adminuser_realname,adminuser_selfconfig,adminuser_isshow,adminuser_language)

sql="select adminuser_name from ce_adminusers where adminuser_name='"&adminuser_name&"'"

ASO.Open sql,conn,1,3

if ASO.RecordCount<>0 then

Response.Redirect("messages.asp?mess_name=DATA_ADD_FAILURE&back_page=adminusers_gl.asp?action=add")

end if

if ASO.RecordCount=0 then

ASO.Close

sql="insert into ce_adminusers(adminuser_name,adminuser_password,admingroup_id,adminuser_realname,adminuser_selfconfig,adminuser_isshow,adminuser_language) values('"&adminuser_name&"','"&adminuser_password&"',"&admingroup_id&",'"&adminuser_realname&"','"&adminuser_selfconfig&"','"&adminuser_isshow&"','"&adminuser_language&"')"

Response.Write(sql)

ASO.Open sql,conn,1,3

Response.Redirect("messages.asp?mess_name=ADD_SUCCESS&back_page=adminusers_gl.asp?action=add")

ASO.Close

end if

End Function

'取得某管理员信息

Public Function AdminUsers_Xx(adminuser_id)

sql="select adminuser_id,adminuser_name,adminuser_password,admingroup_id,adminuser_isshow,adminuser_selfconfig,adminuser_language,adminuser_realname from ce_adminusers where adminuser_id="&adminuser_id

ASO.Open sql,conn,1,3

if ASO.RecordCount=0 then

Response.Redirect("messages.asp?mess_name=GET_Xx_FAILURE&back_page=adminusers_gl.asp?action=list")

else

adminuser_name=ASO("adminuser_name")

adminuser_password=ASO("adminuser_password")

admingroup_id=ASO("admingroup_id")

adminuser_isshow=ASO("adminuser_isshow")

adminuser_selfconfig=ASO("adminuser_selfconfig")

adminuser_language=ASO("adminuser_language")

adminuser_realname=ASO("adminuser_realname")

end if

ASO.close

End Function

'修改某管理员

Public Function AdminUsers_Modideal(adminuser_id,adminuser_name,adminuser_password,admingroup_id,adminuser_isshow,adminuser_selfconfig,adminuser_language,adminuser_realname)

sql="update ce_adminusers set adminuser_name='"&adminuser_name&"',adminuser_password='"&adminuser_password&"',admingroup_id="&admingroup_id&",adminuser_isshow='"&adminuser_isshow&"',adminuser_selfconfig='"&adminuser_selfconfig&"',adminuser_language='"&adminuser_language&"',adminuser_realname='"&adminuser_realname&"' where adminuser_id="&adminuser_id

response.Write(sql)

ASO.Open sql,conn,1,3

Response.Redirect("messages.asp?mess_name=MODIFY_SUCCESS&back_page=adminusers_gl.asp?action=list")

ASO.Close

End Function

'取得管理员列表

Public Function AdminUsers_List()

sql="SELECT ce_admingroups.admingroup_name, ce_adminusers.adminuser_id,ce_adminusers.adminuser_name, ce_adminusers.adminuser_logintimes, ce_adminusers.adminuser_isshow FROM ce_admingroups INNER JOIN ce_adminusers ON ce_admingroups.admingroup_id = ce_adminusers.admingroup_id"

ASO.Open sql,conn,1,3

while not ASO.eof

if ASO("adminuser_isshow")="Y" then

show="是"

else

show="否"

end if

response.write"<tr><td align=right class=td1 width=27% height=25><p align=center>"&ASO("adminuser_name")&"</td><td width=30% align=right class=td1><div align=center>"&ASO("admingroup_name")&"</div></td><td width=13% align=right class=td1><div align=center>"&ASO("adminuser_logintimes")&"</div></td><td width=10% height=25 class=td2><div align=center>"&show&"</div></td><td width=10% class=td2><div align=center><a href=adminusers_gl.asp?action=modi&adminuser_id="&ASO("adminuser_id")&">修改</a></div></td><td width=10% class=td2><div align=center><a href=adminusers_gl.asp?action=del&adminuser_id="&ASO("adminuser_id")&">删除</a></div></td></tr>"

ASO.movenext

wend

ASO.Close

End Function

'删除管理员

Public Function AdminUsers_Del(adminuser_id)

sql="delete from ce_adminusers where adminuser_id="&adminuser_id

ASO.Open sql,conn,1,3

Response.Redirect("messages.asp?mess_name=DEL_SUCCESS&back_page=adminusers_gl.asp?action=list")

ASO.Close

End Function

'获取所有管理员组列表

Public Function Get_AdminGroups()

sql="select admingroup_id,admingroup_name from ce_admingroups where admingroup_language='"&LANGUAGE&"' order by admingroup_id asc"

ASO.Open sql,conn,1,3

response.Write("&nbsp;<select name=admingroup_id>")

while not ASO.eof

response.Write("<option value="&ASO("admingroup_id")&">"&ASO("admingroup_name")&"</option>")

ASO.movenext

wend

response.Write("</select>")

ASO.Close

End Function

'获取所有管理员组列表,并选中当前所在管理员组

Public Function Get_AdminGroups_S()

sql="select admingroup_id,admingroup_name from ce_admingroups where admingroup_language='"&LANGUAGE&"' order by admingroup_id asc"

ASO.Open sql,conn,1,3

response.Write("&nbsp;<select name=admingroup_id>")

while not ASO.eof

if ASO("admingroup_id")=admingroup_id then

s="selected"

else

s=""

end if

response.Write("<option value="&ASO("admingroup_id")&" "&s&">"&ASO("admingroup_name")&"</option>")

ASO.movenext

wend

response.Write("</select>")

ASO.Close

End Function

'公用属性,供外部程序调用

Public Property Get AUName

AUName = adminuser_name

End Property

Public Property Get AUPassword

AUPassword = adminuser_password

End Property

Public Property Get AUGroupid

AUGroupid = admingroup_id

End Property

Public Property Get AUIsshow

AUIsshow = adminuser_isshow

End Property

Public Property Get AUSelfconfig

AUSelfconfig = adminuser_selfconfig

End Property

Public Property Get AURealname

AURealname = adminuser_realname

End Property

End Class

%>

adminusers_gl.asp代码如下:

<%

rem *****************通用网站管理系统***********************************

rem 页面功能:网站后台管理员添加修改表单

rem @author 蔡晓亮

rem @version 1.0

rem @date 2005-2-22

%>

<%

Response.Buffer=true

Response.Expires=0

%>

<!--#include file="../include/common.config.asp"-->

<!--#include file="../include/common.function.asp"-->

<!--#include file="../include/md5.asp"-->

<!--#include file="adminusers_class.asp"-->

<%

action=trim(replace(request("action"),"'",""))

Set GL = New AdminUsersGl '建立类的实例

%>

<%

if action="add" then

adminuser_name=trim(replace(request("adminuser_name"),"'",""))

adminuser_password=md5(trim(replace(request("adminuser_password"),"'","")),32)

admingroup_id=trim(replace(request("admingroup_id"),"'",""))

adminuser_realname=trim(replace(request("adminuser_realname"),"'",""))

adminuser_selfconfig=trim(replace(request("adminuser_selfconfig"),"'",""))

adminuser_isshow=trim(replace(request("adminuser_isshow"),"'",""))

adminuser_language=LANGUAGE

if adminuser_name<>"" then

GL.AdminUsers_Add adminuser_name,adminuser_password,admingroup_id,adminuser_realname,adminuser_selfconfig,adminuser_isshow,adminuser_language

end if

end if

if action="modi" then

adminuser_id=trim(replace(request("adminuser_id"),"'",""))

GL.AdminUsers_Xx adminuser_id

end if

if action="modideal" then

adminuser_id=trim(replace(request("adminuser_id"),"'",""))

adminuser_name=trim(replace(request("adminuser_name"),"'",""))

adminuser_password_n=trim(replace(request("adminuser_password_n"),"'",""))

adminuser_password_o=trim(replace(request("adminuser_password_o"),"'",""))

admingroup_id=trim(replace(request("admingroup_id"),"'",""))

adminuser_isshow=trim(replace(request("adminuser_isshow"),"'",""))

adminuser_selfconfig=trim(replace(request("adminuser_selfconfig"),"'",""))

adminuser_realname=trim(replace(request("adminuser_realname"),"'",""))

adminuser_language=LANGUAGE

if adminuser_id<>"" and adminuser_password_n<>"" then

adminuser_password_n=md5(adminuser_password_n,32)

'response.Write(adminuser_password_n)

GL.AdminUsers_Modideal adminuser_id,adminuser_name,adminuser_password_n,admingroup_id,adminuser_isshow,adminuser_selfconfig,adminuser_language,adminuser_realname

end if

if adminuser_id<>"" and adminuser_password_n="" then

'response.Write(adminuser_password_n&"cxl")

GL.AdminUsers_Modideal adminuser_id,adminuser_name,adminuser_password_o,admingroup_id,adminuser_isshow,adminuser_selfconfig,adminuser_language,adminuser_realname

end if

end if

if action="del" then

adminuser_id=trim(replace(request("adminuser_id"),"'",""))

GL.AdminUsers_Del adminuser_id

end if

%>

<%

title="管理员管理"

css="<link rel=stylesheet href=style/css/style.css type=text/css>"

body="<body topmargin=5 leftmargin=5 bgcolor=#555555>"

ShowHeadHtml title,LANGUAGE,css,body '调用公用函数产生头部代码

%>

<%if action="add" then%>

<table cellpadding="0" cellspacing="1" border="0" width="100%" class="border" align=center height="85">

<form name="form1" method="post" action="adminusers_gl.asp?action=add">

<tr>

<td height="50" colspan="2" align="center" bgcolor="#f1f1f1"><font class="title"><b><%=title%></b></font></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>用户名 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_name" type="text" size="40"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>密 码 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_password" type="password" size="40"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>重复密码 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_password2" type="password" size="40"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong><span class="td1">是否有效</span> :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input type="radio" value="Y" name="adminuser_isshow" checked>

是 <input type="radio" value="N" name="adminuser_isshow">否</td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>真实姓名 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_realname" type="text" size="40"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>管理员组 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">

<%

GL.Get_AdminGroups

%>

</td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>备 注 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">

&nbsp;<textarea name="adminuser_selfconfig" cols="60" rows="10"></textarea></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1">&nbsp;</td>

<td width="77%" align="left" bgcolor="#f1f1f1">

&nbsp;<input type="submit" name="submit" value="添加">

&nbsp;<input type="reset" name="reset" value="重置">

</td>

</tr>

</form>

</table>

<%end if%>

<%if action="modi" then%>

<table cellpadding="0" cellspacing="1" border="0" width="100%" class="border" align=center height="85">

<form name="form1" method="post" action="adminusers_gl.asp?action=modideal">

<tr>

<td height="50" colspan="2" align="center" bgcolor="#f1f1f1"><font class="title"><b><%=title%></b></font></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>用户名 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_id" type="hidden" value=<%=adminuser_id%>><input name="adminuser_name" type="text" size="40" value="<%=GL.AUName%>"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>新 密 码 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_password_n" type="password" size="40"><input name="adminuser_password_o" type="hidden" value="<%=GL.AUPassword%>"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>重复新密码 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_password2_n" type="password" size="40"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong><span class="td1">是否有效</span> :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input type="radio" value="Y" name="adminuser_isshow" <%if GL.AUIsshow="Y" then%>checked<%end if%>>

是 <input type="radio" value="N" name="adminuser_isshow" <%if GL.AUIsshow="N" then%>checked<%end if%>>否</td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>真实姓名 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">&nbsp;<input name="adminuser_realname" type="text" size="40" value="<%=GL.AURealname%>"></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>管理员组 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">

<%

GL.Get_AdminGroups_S

%>

</td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1"><strong>备 注 :</strong></td>

<td width="77%" align="left" bgcolor="#f1f1f1">

&nbsp;<textarea name="adminuser_selfconfig" cols="60" rows="10"><%=GL.AUSelfconfig%></textarea></td>

</tr>

<tr align="center">

<td width="23%" height="32" align="right" bgcolor="#f1f1f1">&nbsp;</td>

<td width="77%" align="left" bgcolor="#f1f1f1">

&nbsp;<input type="submit" name="submit" value="添加">

&nbsp;<input type="reset" name="reset" value="重置">

</td>

</tr>

</form>

</table>

<%end if%>

<%

if action="list" then

%>

<table width="100%" align=center border="0" cellspacing="1" cellpadding="0">

<tr><td colspan=6></td></tr>

<tr bgcolor="#999999"><td align=right class=td1 width="27%" height="25">

<p align="center">名称</td>

<td width="30%" align=right class=td1><div align="center">所属组</div></td>

<td width="13%" align=right class=td1><div align="center">登陆次数</div></td>

<td width="10%" height="25" class=td2><div align="center">是否有效</div></td>

<td width="10%" class=td2><div align="center">修改</div></td>

<td width="10%" class=td2><div align="center">删除</div></td>

</tr>

<%

GL.AdminUsers_List

%>

<tr>

<td height="25" colspan="6" align=right bgcolor="#F1F1F1" class=td1>&nbsp;</td>

</tr>

</table>

<%end if%>

<%

Set GL = Nothing

%>

<%

ShowBottomHtml '调用公用函数产生尾部代码

%>

以上程序写的不是很好,但结构还是比较清晰的,在adminusers_gl.asp中分别对action进行判断,去调用类中的不同方法,在下面页面部分,尽量使ASP代码减少,这样美工就能很轻松的修改程序界面了!

如果你觉得程序写的不好,请不要见笑,欢迎大家和我交流。

我的blog:jblog.create-e.org QQ:5226983

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有