MVC
MVC(Model View Controller)模型-视图-控制器
MVC与模板概念的理解
MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器。使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。比如一批统计数据你可以分别用柱状图、饼图来表示。C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新。 
  模型-视图-控制器(MVC)是Xerox PARC在八十年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已被广泛使用。最近几年被推荐为Sun公司J2EE平台的设计模式,并且受到越来越多的使用 ColdFusion 和 PHP 的开发者的欢迎。模型-视图-控制器模式是一个有用的工具箱,它有很多好处,但也有一些缺点。
MVC如何工作
  MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务。 
  视图 
  视图是用户看到并与之交互的界面。对老式的Web应用程序来说,视图就是由HTML元素组成的界面,在新式的Web应用程序中,HTML依旧在视图中扮演着重要的角色,但一些新的技术已层出不穷,它们包括Macromedia Flash和象XHTML,XML/XSL,WML等一些标识语言和Web services. 
  如何处理应用程序的界面变得越来越有挑战性。MVC一个大的好处是它能为你的应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,不管这些数据是联机存储的还是一个雇员列表,作为视图来讲,它只是作为一种输出数据并允许用户操纵的方式。 
  模型 
  模型表示企业数据和业务规则。在MVC的三个部件中,模型拥有最多的处理任务。例如它可能用象EJBs和ColdFusion Components这样的构件对象来处理数据库。被模型返回的数据是中立的,就是说模型与数据格式无关,这样一个模型能为多个视图提供数据。由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。 
  控制器
  控制器接受用户的输入并调用模型和视图去完成用户的需求。所以当单击Web页面中的超链接和发送HTML表单时,控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,然后确定用哪个视图来显示模型处理返回的数据。 
  现在我们总结MVC的处理过程,首先控制器接收用户的请求,并决定应该调用哪个模型来进行处理,然后模型用业务逻辑来处理用户的请求并返回数据,最后控制器用相应的视图格式化模型返回的数据,并通过表示层呈现给用户。 
为什么要使用 MVC
  大部分Web应用程序都是用像ASP,PHP,或者CFML这样的过程化(自PHP5.0版本后已全面支持面向对象模型)语言来创建的。它们将像数据库查询语句这样的数据层代码和像HTML这样的表示层代码混在一起。经验比较丰富的开发者会将数据从表示层分离开来,但这通常不是很容易做到的,它需要精心的计划和不断的尝试。MVC从根本上强制性的将它们分开。尽管构造MVC应用程序需要一些额外的工作,但是它给我们带来的好处是无庸质疑的。
  首先,最重要的一点是多个视图能共享一个模型,现在需要用越来越多的方式来访问你的应用程序。对此,其中一个解决之道是使用MVC,无论你的用户想要Flash界面或是 WAP 界面;用一个模型就能处理它们。由于你已经将数据和业务规则从表示层分开,所以你可以最大化的重用你的代码了。
  由于模型返回的数据没有进行格式化,所以同样的构件能被不同界面使用。例如,很多数据可能用HTML来表示,但是它们也有可能要用Macromedia Flash和WAP来表示。模型也有状态管理和数据持久性处理的功能,例如,基于会话的购物车和电子商务过程也能被Flash网站或者无线联网的应用程序所重用。
  因为模型是自包含的,并且与控制器和视图相分离,所以很容易改变你的应用程序的数据层和业务规则。如果你想把你的数据库从MySQL移植到Oracle,或者改变你的基于RDBMS数据源到LDAP,只需改变你的模型即可。一旦你正确的实现了模型,不管你的数据来自数据库或是LDAP服务器,视图将会正确的显示它们。由于运用MVC的应用程序的三个部件是相互独立,改变其中一个不会影响其它两个,所以依据这种设计思想你能构造良好的松偶合的构件。
  对我来说,控制器的也提供了一个好处,就是可以使用控制器来联接不同的模型和视图去完成用户的需求,这样控制器可以为构造应用程序提供强有力的手段。给定一些可重用的模型和视图,控制器可以根据用户的需求选择模型进行处理,然后选择视图将处理结果显示给用户。 
MVC的优点
  ◆低耦合性。视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样,一个应用的业务流程或者业务规则的改变只需要改动MVC的模型层即可。因为模型与控制器和视图相分离,所以很容易改变应用程序的数据层和业务规则。
  ◆高重用性和可适用性。随着技术的不断进步,现在需要用越来越多的方式来访问应用程序。MVC模式允许你使用各种不同样式的视图来访问同一个服务器端的代码。它包括任何WEB(HTTP)浏览器或者无线浏览器(wap),比如,用户可以通过电脑也可通过手机来订购某样产品,虽然订购的方式不一样,但处理订购产品的方式是一样的。由于模型返回的数据没有进行格式化,所以同样的构件能被不同的界面使用。例如,很多数据可能用HTML来表示,但是也有可能用WAP来表示,而这些表示所需要的仅令是改变视图层的实现方式,而控制层和模型层无需做任何改变。
  ◆较低的生命周期成本。MVC使降低开发和维护用户接品的技术含量成为可能。
  ◆快速的部署。使用MVC模式使开发时间得到相当大的缩减,它使程序员(Java开发人员)集中精力于业务逻辑,界面程序员(HTML和JSP开发人员)集中业务于表现形式上。
  ◆可维护性。分熟视图层和业务逻辑层也使得WEB应用更易于维护和修改。
  ◆有利于软件工程化管理。由于不同的层各司其职,每一层不同的应用具有某些相同的特征,有利于通过工程化、工具化管理程序代码。 
MVC的缺点
  MVC的缺点是由于它没有明确的定义,所以完全理解MVC并不是很容易。使用MVC需要精心的计划,由于它的内部原理比较复杂,所以需要花费一些时间去思考。
  你将不得不花费相当可观的时间去考虑如何将MVC运用到你的应用程序,同时由于模型和视图要严格的分离,这样也给调试应用程序到来了一定的困难。每个构件在使用之前都需要经过彻底的测试。一旦你的构件经过了测试,你就可以毫无顾忌的重用它们了。
  根据开发者经验,由于开发者将一个应用程序分成了三个部件,所以使用MVC同时也意味着你将要管理比以前更多的文件,这一点是显而易见的。这样好像我们的工作量增加了,但是请记住这比起它所能带给我们的好处是不值一提。
  MVC并不适合小型甚至中等规模的应用程序,花费大量时间将MVC应用到规模并不是很大的应用程序通常会得不偿失。
  MVC设计模式是一个很好创建软件的途径,它所提倡的一些原则,像内容和显示互相分离可能比较好理解。但是如果你要隔离模型、视图和控制器的构件,你可能需要重新思考你的应用程序,尤其是应用程序的构架方面。如果你肯接受MVC,并且有能力应付它所带来的额外的工作和复杂性,MVC将会使你的软件在健壮性,代码重用和结构方面上一个新的台阶。 
开发方式
  Java开发Web Application有几种符合MVC设计模式的开发方式。 
  1:Jsp+Servlet+JavaBean(EJB) 
  2:Jsp+JavaBean(Controller)+JavaBean(EJB)(Model) 
  3:TDK(Turbine,Velocity...)
  4:Xsp 5:Jsp+Struts+JavaBean(EJB) 
  .NET开发Web Application可以采用:
  1: ASP.NET MVC Framework(ASP.NET MVC Beta版)
  2:MonoRail (RC3)
  php 开发Web Application 可以采用:
  1. Zend framework PHP官方框架
  2. fleaphp/Qeephp 等国内流行框架
  3.CakePHP 等国外流行框架 
常见的MVC组件
  Struts Apache的,最流行的MVC组件
  Struts2 Apache用Struts 和 WebWork的组合出来的新产品,目前上升势头强劲
  WebWork 这个可是老牌的MVC组件,后来组合成了Struts2, 不过自身仍在发展
  Spring MVC, SpringFramework自己整合自己Spring的优势推出的MVC组件,用户也不少
  JSF 这个是一个规范,Sun的和 Apache的都有各自的实现。用户量很大,被众多IDE支持。

解决方案 »

  1.   

    MVC 
    MVC (Model View Controller) Model - View - Controller 
    MVC with a template to understand the concept of 
    MVC was originally exist in the Desktop program, M is the data model, V is the user interface, C is the controller. The purpose of the use of MVC is the realization of M and V code separation, so that a program can be used with different forms. For example, a number of statistics you can use histogram, pie chart to show that. C is the purpose of existence is to ensure that the M and V sync, as soon as M changes, V should be updated simultaneously. 
    Model - View - Controller (MVC) is in the eighties Xerox PARC took the Smalltalk-80 programming language invented by a software design pattern, has been widely used. In recent years the company was recommended as the Sun's J2EE platform design patterns, and more and more use of ColdFusion and PHP developers welcome. Model - View - Controller pattern is a useful tool it has many advantages, but there are also some disadvantages. 
    MVC How does it work 
    MVC is a design pattern, it is mandatory to enable the application of input, processing and output separately. The use of MVC applications are divided into three core components: model, view, controller. To deal with their own task. 
    View 
    View is to see and interact with the user interface. Of old-fashioned Web application, the view elements from the HTML interface, the new Web application, HTML is still in view play an important role, but some new technologies have emerged one after another, including Macromedia Flash and as XHTML, XML / XSL, WML up language and some Web services. 
    How to deal with the application interface is becoming increasingly challenging. MVC is a big advantage of it for your application to deal with many different view. In fact, in view of the deal has not really happened, regardless of the data is stored online or list an employee as view is concerned, it is just as an output data and allows users to manipulate way. 
    Model 
    Enterprise data model and business rules. The three components in the MVC, the model has the largest number of processing tasks. For example, it may be used as EJBs and ColdFusion Components This component object to deal with the database. Model data has been returned to neutral, that is, the model has nothing to do with the data format, such a model can provide data for multiple views. Because the code only applies to the model can be written in more than one view of reuse, so a reduction of the repetitive code. 
    Controller 
    Controller to accept user input and call the model and view to the completion of the needs of users. Therefore, when a Web page, click the hyperlink and send HTML form, the controller does not output anything and do anything to deal with. It is just receiving the request and decided to call the component model which deal with the request, and then determine which view to display the returned data model to deal with. 
    Now we sum up the process of MVC, the first controller receives the user's request and decide which model to carry out call processing, and then the model with business logic to handle user requests and return data, the final controller of the view with the corresponding format model to return data, and show that the layer to the user. 
      

  2.   

    Why use MVC to 
    Most Web applications are used as ASP, PHP, or CFML This process of (self-PHP5.0 version has the full support of object-oriented model) to create the language. They will be like a database query of data layers such as HTML code and expression of this mixed layer code. Rich experience in the development of those data from the presentation layer will be separated, but often not very easy, it requires careful planning and continuous attempt. MVC fundamentally separate them mandatory. Although the MVC application structure needs some extra work, but it gives us the advantage of no doubt. 
    First of all, the most important point is that multiple views to share a model, and now need more and more ways to access your application. In this regard, one solution is to use MVC, no matter what you want to Flash the user interface, or WAP interface; will be able to use a model of dealing with them. Because you have the data and business rules separate from the presentation layer, so you can maximize the reuse of your code. 
    Since the model does not return the data format, so the same components can be the use of different interfaces. For example, many data may be expressed as HTML, but they may also use Macromedia Flash and WAP expressed. Model also has a lasting state of management and data processing functions, such as session-based shopping cart and e-commerce Flash web site can be the process of wireless networking or the reuse of applications. 
    Because the model is self-contained and with separation of controller and view, so it is easy to change your application's data layer and business rules. If you want your database from MySQL transplanted to Oracle, or change your RDBMS-based data source into LDAP, just change the model you can. Once you have achieved the correct model, regardless of your data from a database or LDAP server, the view will display them correctly. MVC as a result of the application to use the three components are mutually independent, one change will not affect the other two, so the ideological basis for this design a good structure you can loose coupling of components. 
    For me, the controller also provides a benefit, that is, the controller can be used to connect different models and views to complete the needs of users, so that the controller application can be constructed to provide a strong means. Given a number of reusable models and view, controller in accordance with the needs of the users choice of models to deal with, and then choose to view the results displayed to the user. 
    The benefits of MVC 
    ◆ low coupling. View of the separation layer and business layer, so that the code allowed to change the view layer without having to recompile the model and controller code, and similarly, an application of business processes or changes in business rules need to change the model layer to MVC. Because the model with the controller and the view of phase separation, so it is easy to change the application's data layer and business rules. 
    ◆ high reusability and applicability. With the continuous advancement of technology, and now need more and more ways to access applications. The use of MVC pattern allows you to view a variety of different styles to access the same server-side code. It includes any WEB (HTTP) browser or wireless browser (wap), for example, computer users can also be ordered through a mobile phone-like products, although not the same as ordered by the way, but the handling of orders is the same way. Since the model does not return the data format, so the same components of the interface can be different. For example, many data may be expressed as HTML, but may also be expressed by WAP, which required only that the layer is to change the view of the realization of the way, and the control layer and model layer without any change. 
    ◆ lower life-cycle costs. MVC enable users to reduce development and maintenance of the technological content of access possible. 
    ◆ rapid deployment. The use of MVC pattern so that development time has been reduced considerably, which allows programmers (Java developers) focus on business logic, interface programmer (HTML and JSP developers) focus on the performance of formal business. 
    ◆ maintainability. View sub-cooked layer and Business Logic Tier WEB application also makes it easier to maintain and modify. 
    ◆ in favor of software engineering management. Because each different layer, each layer of different applications have some similar characteristics, is conducive to the adoption of the project and code management tools. 
    The shortcomings of MVC 
    The shortcomings of MVC because it has not been clearly defined, so fully understand the MVC is not easy. The use of MVC needs careful planning, because of its complex internal principle, so take some time to think. 
    You will have to spend considerable time to consider how to use MVC to your application, at the same time as a result of model and view to the strict separation of the debug such applications also come with certain difficulties. Each component requires the use of thoroughly before testing. Once you have tested the components, you can reuse with impunity them. 
    According to the experience of developers, due to the development of an application will be divided into three parts, so the use of MVC at the same time also means that you will more than ever before to manage documents, it is obvious. This is a increase in our workload, but please remember this than it can give us the advantage of not worth mentioning. 
    MVC is not suitable for small or medium-scale applications, MVC will take much more time will be applied to large-scale applications are not normally be wasted. 
    MVC design pattern is a good way to create software, it is advocated by a number of principles, such as separation of content and show each other may be better understood. However, if you want to isolate the model, view and controller components, you may need to rethink your application, especially in regard to the application of the framework. If you are willing to accept the MVC, and it has the ability to cope with the additional complexity of the work and, MVC will make your software robustness, and structural aspects of code reuse to a new level. 
    Development methods 
    Web Application Development Java there are several design patterns in line with the MVC development. 
    1: Jsp + Servlet + JavaBean (EJB) 
    2: Jsp + JavaBean (Controller) + JavaBean (EJB) (Model) 
    3: TDK (Turbine, Velocity ...) 
    4: Xsp 5: Jsp + Struts + JavaBean (EJB) 
    . NET Development Web Application can be used: 
    1: ASP.NET MVC Framework (ASP.NET MVC Beta version) 
    2: MonoRail (RC3) 
    Php can be used to develop Web Application: 
    1. Zend framework PHP official framework 
    2. Fleaphp / Qeephp popular framework for domestic 
    Popular framework for foreign 3.CakePHP 
    MVC common components 
    Struts Apache, the most popular MVC components 
    Struts2 Apache to use a combination of Struts and WebWork out new products, the current upward trend of strong 
    WebWork style of the MVC components, however, and later became a portfolio Struts2, but are still developing their own 
    Spring MVC, SpringFramework own Spring integration advantages of its own launch of MVC components, a number of users 
    The JSF is a specification, Sun's and Apache's have their own implementations. Large volume users, the number of IDE support.