Tuesday, January 8, 2013

MVC1 Vs MVC2

  • from stackoverflow...
    The first Java technology for server-side web development was the servlet.
    Writing applications with servlets was very similar to writing CGI applications in Perl in that all of the output had to be built up as Strings from within Java code.
    This was very tedious and error-prone. It also made it very difficult for web designers with no Java experience to alter the look and feel of the pages generated by servlets.
    alt text
    Then came JSP. JSPs, like Microsoft ASPs and like the popular scripting language PHP, treat everything as template text, but allow the insertion of Java code into tags called scriptlets and JSP expressions.
    This allowed people to work on server-side applications just as they would with the other popular scripting languages but it had a couple of drawbacks.
    • There was no separation of concerns.
    • One script would hold database code, business logic, HTML markup and any javascript code needed for the final page rendering.
    • Code reuse was difficult as was automated testing.
    This came to be known as "Model 1" JSP programming.
    MVC or the Model View Controller pattern was a common technique for separating the various concerns in GUI code invented by Trygve Reenskaug, working on Smalltalk for Zerox.
    At some point it became clear that this technique could be adapted to Java EE applications to achieve the same level of separation.
    Doing so involves writing the Model layer as Beans or Plain Old Java Objects (POJOs), using servlets as the Controller, and then, when all the heavy lifting is done, forwarding to a JSP to format and markup the results.
    Servlet/JSP applications written using and MVC architecture came to be known as Model 2 JSP programming.
    alt text
    Because this pattern existed in a different form before being used in servlet/JSP applications, it was sometimes referred to as "MVC2". This name led to some confusion as it implied that there is an MVC1 for servlet applications, which there is not.
    It is sufficient just to say MVC.
    from javabeat...
    A Model 1 architecture consists of a Web browser directly accessing Web-tier JSP pages. The JSP pages access Web-tier JavaBeans that represent the application model, and the next view to display (JSP page, servlet, HTML page, and so on) is determined either by hyperlinks selected in the source document or by request parameters. A Model 1 application control is decentralized, because the current page being displayed determines the next page to display. In addition, each JSP page or servlet processes its own inputs (parameters from GET or POST). In some Model 1 architectures, choosing the next page to display occurs in scriptlet code, but this usage is considered poor form. (See the design guideline Section 4.2.6.8 on page 89.)

    A Model 2 architecture introduces a controller servlet between the browser and the JSP pages or servlet content being delivered. The controller centralizes the logic for dispatching requests to the next view based on the request URL, input parameters, and application state. The controller also handles view selection, which decouples JSP pages and servlets from one another. Model 2 applications are easier to maintain and extend, because views do not refer to each other directly. The Model 2 controller servlet provides a single point of control for security and logging, and often encapsulates incoming data into a form usable by the back-end MVC model. For these reasons, the Model 2 architecture is recommended for most interactive applications.


    from theserverside...


    MVC1 was a first generation approach that used JSP pages and the JavaBeans component architecture to implement the MVC architecture for the Web. HTTP requests are sent to a JSP page that implements Controller logic and calls out to the “Model” for data to update the “View.” This approach combines Controller and View functionality within a JSP page and therefore breaks the MVC paradigm. MVC1 is appropriate for simple development and prototyping. It is not, however, recommended for serious development. 

    MVC2 is a term invented by Sun to describe an MVC architecture for Web-based applications in which HTTP requests are passed from the client to a “Controller” servlet which updates the “Model” and then invokes the appropriate “View” renderer-for example, JSP technology, which in turn renders the View from the updated Model. 
    The hallmark of the MVC2 approach is the separation of Controller code from 
    content. (Implementations of presentation frameworks such as Struts, adhere to the MVC2 approach). 

No comments:

Post a Comment