📄 设计模式:设计自己的mvc框架(转) - 陋室铭 - 博客园.htm
字号:
Library 4.0将支持依赖注入</A>
<LI><A
id=SingleColumn1_ctl06_RecentPostsList_ctl12_RecentPostsList2_ctl00_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/04/25/1171384.html">20. 七种武器——.NET工程师求职面试必杀技(转,有道理)</A>
</LI></UL></DIV>
<SCRIPT language=JavaScript>
function SearchGoogle(key,evt,site)
{
if(evt.keyCode==13 || evt.keyCode==0 || evt.type =='click')
{
key.focus();
var keystr = encodeURIComponent(key.value);
url = "http://www.google.com/search?q=";
url = url+keystr;
url += "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains="+site+"&sitesearch="+site;
window.location=url;
return false;
}
}
</SCRIPT>
<H3>搜索</H3>
<UL>
<LI>
<LI><INPUT id=q
onkeydown="return SearchGoogle(document.getElementById('q'),event,'http://www.cnblogs.com/cuihongyu3503319/')"
style="WIDTH: 130px" name=q> <INPUT onclick="SearchGoogle(document.getElementById('q'),event,'http://www.cnblogs.com/cuihongyu3503319/')" type=button value=搜索 name=sa>
</LI></UL>
<H3>积分与排名</H3>
<UL>
<LI>积分 - 83693
<LI>排名 - 429 </LI></UL>
<H3>最新评论 <A id=SingleColumn1__80666e_RSSHyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/CommentsRSS.aspx"><IMG
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"
src="设计模式:设计自己的MVC框架(转) - 陋室铭 - 博客园.files/xml.gif"></A></H3>
<DIV class=RecentComment id=RecentComments>
<UL>
<LI style="WORD-BREAK: break-all"><A
id=SingleColumn1__80666e_CommentList_ctl01_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/07/15/561935.html#1255694">1. re:
后台一行输入太多内容,使前台显示自动换行方法</A>
<LI style="WORD-BREAK: break-all">谢谢啦。 <BR><BR>无情的偷走了, <BR>呵呵
<LI style="MARGIN-RIGHT: 4px; TEXT-ALIGN: right">--鹏鹏_Lovely
<LI style="WORD-BREAK: break-all"><A
id=SingleColumn1__80666e_CommentList_ctl02_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/25/530382.html#1236161">2. re:
asp.net控件开发基础(9) ----------再谈属性,学习自定义类型转换器</A>
<LI style="WORD-BREAK: break-all">不论如何 太感谢你了 <BR><BR>我的自定义属性第一次实现下拉列表形式( enum
类型的不算 )
<LI style="MARGIN-RIGHT: 4px; TEXT-ALIGN: right">--kaly
<LI style="WORD-BREAK: break-all"><A
id=SingleColumn1__80666e_CommentList_ctl03_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/20/817829.html#1231697">3. re:
sql server2005服务无法启动</A>
<LI style="WORD-BREAK: break-all">非常感谢,解决了我的问题,开始以为是因为没装iis的原因呢。
<LI style="MARGIN-RIGHT: 4px; TEXT-ALIGN: right">--sounder
<LI style="WORD-BREAK: break-all"><A
id=SingleColumn1__80666e_CommentList_ctl04_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/17/530369.html#1227727">4. re:
asp.net控件开发基础(3) ----------自定义控件事件处理</A>
<LI style="WORD-BREAK: break-all">public bool LoadPostData(string postDataKey,
NameValueCollection postCollection) { string postedV...
<LI style="MARGIN-RIGHT: 4px; TEXT-ALIGN: right">--小眼睛老鼠
<LI style="WORD-BREAK: break-all"><A
id=SingleColumn1__80666e_CommentList_ctl05_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/15/980222.html#1226229">5. re:
ASP.Net七大内置对象 (整理的不错,转过来参考) </A>
<LI style="WORD-BREAK: break-all">转载的东西,也不排下格式,下次注意哈,^_^
<LI style="MARGIN-RIGHT: 4px; TEXT-ALIGN: right">--fphuang1 </LI></UL></DIV>
<H3>阅读排行榜</H3>
<DIV class=RecentComment id=TopViewPosts>
<UL style="WORD-BREAK: break-all">
<LI><A id=SingleColumn1__15f0b18_TopList_ctl01_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/03/21/682762.html">1. 什么是中间件?(3277)</A>
<LI><A id=SingleColumn1__15f0b18_TopList_ctl02_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/02/28/659213.html">2. C#
GDI+编程之基础篇(2141)</A>
<LI><A id=SingleColumn1__15f0b18_TopList_ctl03_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/07/20/825336.html">3. ASP.NET操作EXCEL时出现的错误
Retrieving the COM class factory for component with CLSID(转)(1729)</A>
<LI><A id=SingleColumn1__15f0b18_TopList_ctl04_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2006/12/29/606717.html">4. 什么是耦合?
(1588)</A>
<LI><A id=SingleColumn1__15f0b18_TopList_ctl05_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/04/03/698395.html">5. 您不能初始化一个远程桌面连接,因为在远程计算机上的windows登录软件被不兼容的软件取代(1495)</A>
</LI></UL></DIV>
<H3>评论排行榜</H3>
<DIV class=RecentComment id=TopCommentsPosts>
<UL style="WORD-BREAK: break-all">
<LI><A id=SingleColumn1__adc1c9_TopList_ctl01_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/04/29/731905.html">1. C#分页控件(自己做的)(9)</A>
<LI><A id=SingleColumn1__adc1c9_TopList_ctl02_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/02/27/658524.html">2. 软件架构训练之层次及使用(8)</A>
<LI><A id=SingleColumn1__adc1c9_TopList_ctl03_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/03/21/682762.html">3. 什么是中间件?(7)</A>
<LI><A id=SingleColumn1__adc1c9_TopList_ctl04_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/07/20/825336.html">4. ASP.NET操作EXCEL时出现的错误
Retrieving the COM class factory for component with CLSID(转)(6)</A>
<LI><A id=SingleColumn1__adc1c9_TopList_ctl05_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/01/11/617820.html">5. 利用vml制作统计图全攻略(饼图)[转自CSDN](6)</A>
</LI></UL></DIV>
<H3 class=catListTitle>60天内阅读排行</H3>
<DIV class=RecentComment>
<UL>
<LI><A id=SingleColumn1__19dc29a_TopList_ctl01_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/05/29/1210117.html">1. 动态调用
WebService(转)(83)</A>
<LI><A id=SingleColumn1__19dc29a_TopList_ctl02_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/03/1213111.html">2. 哈希算法(83)</A>
<LI><A id=SingleColumn1__19dc29a_TopList_ctl03_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/04/1213946.html">3. 加密技术在企业数据安全中的应用(转)(79)</A>
<LI><A id=SingleColumn1__19dc29a_TopList_ctl04_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/03/1213140.html">4. PKI基础
二.PKI基础--5.数字证书及应用(转,一个加密解密的全过程)(77)</A>
<LI><A id=SingleColumn1__19dc29a_TopList_ctl05_Hyperlink1
href="http://www.cnblogs.com/cuihongyu3503319/archive/2008/06/18/1224861.html">5. 关于IE缓存的解决方案(HTML,JSP,ASP,PHP,C#)(转)(73)</A>
</LI></UL></DIV></DIV></DIV>
<DIV id=centercontent>
<SCRIPT type=text/javascript>
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>
</SCRIPT>
<!--done-->
<DIV class=post>
<DIV class=postTitle><A class=postTitle2 id=AjaxHolder_ctl01_TitleUrl
href="http://www.cnblogs.com/cuihongyu3503319/archive/2007/02/27/658532.html">设计模式:设计自己的MVC框架(转)</A>
</DIV>源代码放在sharesources@126.com的邮箱的收件箱里,用户名:sharesource,密码:javafans<BR>希望保留给有用的人,谢谢。<BR><BR> 取这样一个标题太大,吸引眼球嘛@_@。<BR><BR> 事实是最近读《<A
href="http://www.qqread.com/keywords/j2ee.html" target=_blank><FONT
color=#002c99>J2EE</FONT></A>设计模式》讲述表达层模式的那几章,书中有一个前端控制器+command模式的workflow例子,就琢磨着可以很简单地扩展成一个MVC框架。花了一个下午改写了下,对书中所述的理解更为深入。我想这也许对于学习和理解设计模式,以及初次接触<A
href="http://www.qqread.com/z/soft/struts/" target=_blank><FONT
color=#002c99>struts</FONT></A>等MVC框架的人可能有点帮助。因为整个模型类似于struts,我把它取名叫strutslet^_^。学习性质,切勿认真。<BR><BR>(一)完整的类图如下:<BR><A
href="设计模式:设计自己的MVC框架(转) - 陋室铭 - 博客园.files/nr152_1.gif" target=_blank><IMG
alt=设计模式:设计自己的MVC框架(图一) src="设计模式:设计自己的MVC框架(转) - 陋室铭 - 博客园.files/nr152_1.gif"
width=590 border=0>
<CENTER>点击查看大图</CENTER></A><BR><BR>1。前端控制器(FrontController):前端控制器提供了一个统一的位置来封装公共请求处理,它的任务相当简单,执行公共的任务,然后把请求转交给相应的控制器。在strutslet中,前端控制器主要作用也在于此,它初始化并解析配置文件,接受每个请求,并简单地把请求委托给调度器(Dispatcher),由调度器执行相应的动作(Action)。调度器把action返回的url返回给FrontController,FrontController负责转发。<BR><BR>2。Action接口:command模式很好的例子,它是一个命令接口,每一个实现了此接口的action都封装了某一个请求:新增一条数据记录并更新model,或者把某个文件写入磁盘。命令解耦了发送者和接受者之间联系。 发送者调用一个操作,接受者接受请求执行相应的动作,因为使用Command模式解耦,发送者无需知道接受者任何接口。<BR><BR>3。Dispatcher:调度器,负责流程的转发,负责调用action去执行业务逻辑。由调度器选择页面和action,它去除了应用行为和前端控制器间的耦合。调度器服务于前端控制器,它把model的更新委托给action,又提供页面选择给FrontController<BR><BR>4。ActionForward:封装了转向操作所需要信息的一个模型,包括name和转向url<BR><BR>5。ActionModel:解析配置文件后,将每一个Action封装成一个ActionModel对象,所有ActionModel构成一个map,并<A
href="http://www.qqread.com/z/server/storage/"
target=_blank>存储</A>在ServletContext中,供整个框架使用。<BR><BR>(二)源代码简单分析<BR>1。Action接口,只有一个execute方法,任何一个action都只要实现此接口,并实现相应的业务逻辑,最后返回一个ActionForward,提供给Dispacher调用。<BR>
<DIV class=codeStyle twffan="done">
<OL>
<LI>
<LI><STRONG><FONT color=#0000ff>public</FONT></STRONG> <STRONG><FONT
color=#0000ff>interface</FONT></STRONG> <FONT
color=#ff0000>Action</FONT> {
<LI> <STRONG><FONT
color=#0000ff>public</FONT></STRONG> ActionForward execute(HttpServletRequest request,ServletContext context);
<LI>}
</LI></OL></DIV><BR><BR>比如,我们要实现一个登陆系统(demo的例子),LoginAction验证用户名和密码,如果正确,返回success页面,如果登陆失败,返回fail页面:<BR>
<DIV class=codeStyle twffan="done">
<OL>
<LI>
<LI><STRONG><FONT color=#0000ff>public</FONT></STRONG> <STRONG><FONT
color=#0000ff>class</FONT></STRONG> LoginAction <STRONG><FONT
color=#0000ff>implements</FONT></STRONG> <FONT
color=#ff0000>Action</FONT> {
<LI>
<LI> <STRONG><FONT
color=#0000ff>private</FONT></STRONG> <STRONG><U>String</U></STRONG> name=<FONT
color=#ff33ff>""</FONT>;
<LI> <STRONG><FONT
color=#0000ff>public</FONT></STRONG> ActionForward execute(HttpServletRequest request,
<LI> ServletContext context) {
<LI> <STRONG><U>String</U></STRONG> userName=request.getParameter(<FONT
color=#ff33ff>"userName"</FONT>);
<LI> <STRONG><U>String</U></STRONG> password=request.getParameter(<FONT
color=#ff33ff>"password"</FONT>);
<LI> <STRONG><FONT
color=#0000ff>if</FONT></STRONG>(userName.equals(<FONT
color=#ff33ff>"dennis"</FONT>)&&password.equals(<FONT
color=#ff33ff>"123"</FONT>)){
<LI> request.setAttribute(<FONT
color=#ff33ff>"name"</FONT>, name);
<LI> <STRONG><FONT
color=#0000ff>return</FONT></STRONG> ActionForward.SUCCESS; <EM><FONT
color=#339900>//登陆成功,返回success</FONT></EM>
<LI> }<STRONG><FONT
color=#0000ff>else</FONT></STRONG>
<LI> <STRONG><FONT
color=#0000ff>return</FONT></STRONG> ActionForward.FAIL; <EM><FONT
color=#339900>//否则,返回fail</FONT></EM>
<LI> } </LI></OL></DIV>
<P><BR> </P>
<P>2.还是先来看下两个模型:ActionForward和ActionModel,没什么东西,属性以及相应的getter,setter方法:<BR></P>
<P> </P>
<DIV class=codeStyle twffan="done">
<OL>
<LI>
<LI><EM><FONT color=#339900>/**</FONT></EM>
<LI><EM><FONT color=#339900> * 类说明:转向模型</FONT></EM>
<LI><EM><FONT color=#339900> * @author dennis</FONT></EM>
<LI><EM><FONT color=#339900> *</FONT></EM>
<LI><EM><FONT color=#339900> * */</FONT></EM>
<LI><STRONG><FONT color=#0000ff>public</FONT></STRONG> <STRONG><FONT
color=#0000ff>class</FONT></STRONG> ActionForward {
<LI> <STRONG><FONT
color=#0000ff>private</FONT></STRONG> <STRONG><U>String</U></STRONG> name; <EM><FONT
color=#339900>//forward的name</FONT></EM>
<LI> <STRONG><FONT
color=#0000ff>private</FONT></STRONG> <STRONG><U>String</U></STRONG> viewUrl; <EM><FONT
color=#339900>//forward的url</FONT></EM>
<LI> <STRONG><FONT
color=#0000ff>public</FONT></STRONG> <STRONG><FONT
color=#0000ff>static</FONT></STRONG> <STRONG><FONT
color=#0000ff>final</FONT></STRONG> ActionForward SUCCESS=<STRONG><FONT
color=#0000ff>new</FONT></STRONG> ActionForward(<FONT
color=#ff33ff>"success"</FONT>);
<LI> <STRONG><FONT
color=#0000ff>public</FONT></STRONG> <STRONG><FONT
color=#0000ff>static</FONT></STRONG> <STRONG><FONT
color=#0000ff>final</FONT></STRONG> ActionForward FAIL=<STRONG><FONT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -