现在的位置: 首页C#>正文
ASP.NET MVC模式及MVC1和MVC2模式的区别
2009年12月17日 C# 评论数 1 ⁄ 被围观 阅读(2,857)+

    MVC模式是"Model-View-Controller"的缩写,中文翻译为"模式-视图-控制器"。MVC应用程序总是由这三个部分组成。Event(事件)导致Controller改变Model或View,或者同时改变两者。只要Controller改变了Models的数据或者属性,所有依赖的View都会自动更新。类似的,只要Controller改变了View,View会从潜在的Model中获取数据来刷新自己。

如下图:

File:ModelViewControllerDiagramZh.png

MVC模式在具体的应用中,又分为MVC1和MVC2模式。下面分别说说这两种模式的概念、应用方向和主要区别。

1.MVC1模式

View接受用户输入,并传递到Controller。

Controller统一进行处理命令,交由Model处理具体的业务。

经过处理Model更新后,通知View进行更新。

这种模式主要用于桌面程序,使用观察者模式实现,具体来说就是让View观察Model,而用户交互控制的地方用匿名类的方式统一放在Controller中。像MFC的frame-document-view架构,如果document改变了,他会主动通知View进行update。但是在WEB应用程序中,Model(在Java中通常是JavaBean)的数据更新后,无法通知View进行更新(View在Java中通常是非常多JSP页面,需要选择一个),因此,在WEB应用程序中应该使用下面这种模式:MVC2模式

2.MVC2模式

View接受用户输入,并传递到Controller。

Controller统一进行处理命令,交由model处理具体的业务。

经过处理的Model更新后,Controller会选一个View并把Model内容传递(request,session)给它(forward)。然后View进行显示。

3.区别

可以看到,Model是否能主动通知View就是MVC1和MVC2模式的主要差别。桌面程序可以做到,所以使用MVC1;Web程序由于Http协议的限制做不到,所以使用MVC2。

【上篇】
【下篇】

目前有 1 条留言 其中:访客:1 条, 博主:0 条

  1. 匿名 : 2010年03月16日16:25:29  1楼 @回复 回复

    [face=19]

给我留言

留言无头像?


[face=9] [face=8] [face=7] [face=6] [face=5] [face=4] [face=3] [face=30] [face=2] [face=29] [face=28] [face=27] [face=26] [face=25] [face=24] [face=23] [face=22] [face=21] [face=20] [face=1] [face=19] [face=18] [face=17] [face=16] [face=15] [face=14] [face=13] [face=12] [face=11] [face=10] [em=9] [em=8] [em=7] [em=6] [em=5] [em=4] [em=3] [em=30] [em=2] [em=29] [em=28] [em=27] [em=26] [em=25] [em=24] [em=23] [em=22] [em=21] [em=20] [em=1] [em=19] [em=18] [em=17] [em=16] [em=15] [em=14] [em=13] [em=12] [em=11] [em=10] ;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)