mvc模式总在嘴边说,可是mvc 2我还是最近听说。在此我谈谈我的它的理解。如有不对,请各位同行斧正!
mvc既model - view - control.view:既jsp,html,servlet...显示于用户的数据。control控制器,即对业务流程的控制,view跳转的控制等。model即模型,程序运行中的数据。
mvc2与mvc不同之处在于model,mvc2的model功能更智能,更强大。主要体现在mvc2的medol可以对view进行通知和调用。这个是mvc不具备的功能。传统的mvc,只能是view处罚业务逻辑,来改变model的数据。
例如:当model的数据改变了,model本身能够通知到view,让view做响应的操作。
以上是我对mvc的理解,不知道是否正确?请大家别客气,有问题请直接指正!
MVC 与MVC2 最大的区别是,MVC(1)它是胖客户端的,大量的业务逻辑是写在JSP中的,这样会造成代码维护,响应速度,最终要的是在进行中,大规模设计开发时很难惊醒模块化设计,
而MVC2 的最大改进就是把JSP SERVLET JAVABEAN 进行各个任务的功能分离,就是大家常见到各种MVC的解释
MVC2的来源的描述:
In the traditional use of MVC like that used in Smalltalk, the Model notifies the View components when changes occur to the state of the Model and the view queries the Model for the state information. With web applications, this is difficult since a browser typically functions as a pull-architecture rather than a push. When the state of an entity bean changes for example, a browser application is generally not aware of the changes until it "pulls" the changes into the browser. This small difference between the traditional definition of MVC and how it's used within web applications has caused some to refer to the new way as Web MVC or MVC2.
mvc模式:
view接收用户输入,把命令传到controller
controller处理命令,更新model
model被更新后,会通知view需要update
view更新后向用户显示
mvc2模式:
由于mvc1中,model可以通知view,然后view就可以被更新,这在windows程序中很常见,像MFC的frame-document-view架构,如果document改变了,他会主动通知view进行update
但是在web中,作为model的java bean更新后,无法调用作为view的jsp(而且view通常很多,需要选一个),因此改为mvc2模式:
view接收用户输入,把命令传到controller
controller处理命令,更新model
model被更新后,controller会选一个view并forward到这个jsp,附带把model放到request参数
这个view获得model然后显示
看看这文章写得很好
http://williamliu.iteye.com/blog/361628
[quote]view:既jsp,html,servlet...显示于用户的数据[/quote]
servlet其实是控制器
[quote]control控制器,即对业务流程的控制,view跳转的控制等。[/quote]
[quote]model即模型,程序运行中的数据。[/quote]
[quote]例如:当model的数据改变了,model本身能够通知到view,让view做响应的操作。[/quote]
在swing的MVC中 可以做到
其实典型的MVC在swing中
比如JTree
就有
TreeModel=模型
事件监听=控制器
JTree=视图
最后希望及时结帖啊 ,我又落后了 :oops:
这个比较好解释正好我所开发的而就是基于这种开发方式,其实现在很多的mvc框架都又往这种方向发展的趋势。
先来说一下以前的mvc
m:model 是模型,用于数据绑定等。其说说成formBean比较合适(struts里)
v:view 表现层,就是表现形式。
c:controller 控制器,用来的去view层传回来的数据,也向controller返回数据。
基于传统mvc呢,struts呢我们的model要继承formBean的。
要了解mvc2呢,你要了解一下这个情况:
mvc 开发呢,我们还会有个Pojo用户与数据库持久化的model.
当mvc2 的时候呢我们的开发就不必可以的去分别这个model是formBean还是hibernaet等orm映射与数据库持久化的pojo了,这里给你几个示例代码:
1 view 填写入数据
[code="java"]
pojo(hibernate ORM的pojo)
[code="java"]
@Entity
@Table(name = "users")
public class User implements UserDetails{
@id
private Integer userId;
private String username;
get set略写......
[/code]
Controller
[code="java"]
@Controller
public class LoginLogController {
@Autowired
UserService userService;
@RequestMapping(value="user.do")
public String getProv(User userTemp,ModelMap model){
User user = userService.getUser(user.getId);
user.setName(userTemp.getName());
userService.upData(user);
return "/user.do";
}
}
[/code]
以上的带么用的spring mvc 我没有为userController些一个formbean,而是用了个User user 来接前台传回来的 参数userId,userName,我们要做的就是
里的input域的name 与User类里又相应的name就可以!这也就是大家非常推崇的绑定技术!其实不只是hibernate的pojo可以作为formBean,你自己随便些一个pojo都可以,当然要又属性与get set方法!这样更便于开发,有的时候一个项目里,基本上写一两个通用bean作为formBean就可以了,如果和hibernate的pojo一致基本上不用些!如果mvc写controller的话,我们多做点事,你要为你的action设计一个对应的formBean。
现在完全没有必要将http完全透明了,你看我设计的controller里面不带有HttpServlet response,HttpServlet request等。
前台传回的userId与userName
也不用些getParameter也不用设计个formBean