Implementation of the MVC pattern in Java Web applications

Source: Internet
Author: User
Tags grails java web

I. Introduction to MVC

  The MVC (Model-view-controller) Model-View-controller, first proposed by Trygve Reenskaug in 1978, is the Xerox Palo Alto Research Center (Xerox PARC) a software architecture invented in the 1980s for the programming language Smalltalk. The purpose of the MVC pattern is to implement a dynamic program design that simplifies the subsequent modification and expansion of the program, and makes it possible to reuse a part of the program. In addition, this mode makes the program structure more intuitive by simplifying the complexity. The software system is separated by the basic parts of itself and also gives the functions of the basic parts.

    • Model: used to encapsulate data related to the business logic of an application and how data is processed. Models have the power to access data directly, such as access to a database. Models do not depend on views and controllers, that is, the M model does not care how it will be displayed or manipulated. However, changes in the data in the model are generally advertised through a refresh mechanism. To implement this mechanism, the views that are used to monitor this model must be registered in advance on this model, so that the view can understand the changes that have occurred on the data model.
    • View: enables the display of data for a purposeful purpose. There is generally no logic on the program in the view. To implement the Refresh feature on the view, the view needs to access the data model it monitors, so it should be registered with the data it is monitoring beforehand.
    • Controller: acts as an organizational function between different levels to control the flow of applications. It handles the event and responds. "Events" includes user behavior and changes on the data model

In application programming in SMALLTALK-80: How to use Model-view-Controller (MVC) [Burbeck92], Steve Beubecques (Steve Burbeck) describes two variants of MVC: A passive model and an active model.

    • Passive model: A passive model is used when a controller specializes in manipulating the model. The controller modifies the model, and then notifies the view that the model has changed and should be updated. the model in this case is completely independent of the view and the controller, which means that the model cannot report changes in its state. The HTTP protocol is the example. There is no easy way to get an asynchronous update from the server in the browser. The browser displays the view and responds to user input, but does not detect changes to the data on the server. The server is asked to make changes only when the user explicitly requests a refresh.

  

    • Active Model: The active model is used when the model changes state without the involvement of the controller. This can happen when other sources change the data and the changes must be reflected in the view. Consider the ticker symbol display. You can receive stock data from external sources and want to update the view (for example, stock quotes and alert windows) when the stock data changes. because only the model detects changes in the internal state when they occur, the model must notify the view to refresh the display.

  

Ii. Common components of Java Web applications

1.Serverlet

    The servlet runs in a web container, such as Tomcat, which can be dynamically loaded by the Web container, receiving browser requests, invoking other components to process requests, and then returning the processing results. When a browser accesses a servlet, the Web container creates a ServletRequest object and a Servletresponse object, and encapsulates the user's request information in the ServletRequest object. The two objects are then transferred as parameters to the specific method of the servlet. In this method, the request is processed, the processing result is encapsulated in the Servletresponse object, and returned to the Web container. Finally, the Web container returns the results to the browser to parse and display.

  2.Jsp

    JSP is a Dynamic Web page technology. It adds the HTML page to the Java script, and the JSP tag forms the JSP file. When a browser requests a JSP page, Tomcat translates the JSP page into a Java file. It then compiles it into a class file, executes it, and returns the resulting HTML page to the client display.

  3.JavaBean

   JavaBeans is a special class in Java that can encapsulate multiple objects into an object (bean). The feature is serializable, providing a parameterless constructor that provides getter methods and setter methods to access the properties of the object.

  4.EJB

    Enterprise JavaBean, EJB, JavaBean is a server-side manageable component that is used to build enterprise-class applications. The specification for EJBS is provided in the Java Enterprise Edition API (Edition). EJB is a server-side component that encapsulates a business logic that has an application.

 5.Web Server and Application server

The Web server is used to process HTTP requests and return HTTP data. It is mainly used to interact with the browser to provide services to the user. The application server can be called by the Web server to handle the business logic. The data it returns is organized into HTML format by the Web server and then returned to the browser to parse.

Third, Java Web Solutions (development method)

1.jsp+javabean: This mode separates the business logic from the performance of the page, and increases the debugging and maintainability of the program to some extent. Simple, suitable for quick construction and operation of small projects.

2.jsp+javabean+serverlet: The JSP acts as a view to represent the page; The servlet acts as a controller, the process of controlling the program and invokes the business; JavaBean encapsulates the business logic. Followed the MVC design pattern.

3.jsp+struts: A mature MVC development framework.

4.Spring: built on core modules, tender enough to adapt to a variety of multi-view, template technology, internationalization and validation services to achieve a clear separation of control logic and business logic.

5.jsp+struts+hibernate: A development scenario that leverages the MVC design pattern of Struts with Hibernate persistence objects.

6.jsp+struts+spring+hibernate: Struts is responsible for the presentation layer, and spring is responsible for the business of the logical layer, Hibernate persistence layer database operation, composed of development scenarios.

Iv. Common Java Web MVC framework

1.struts2: Struts is an open source project sponsored by the Apache Software Foundation (ASF). It was originally a subproject in the Jakarta Project and became the top-level project for ASF in March 2004. Using javaservlet/jsp technology, it realizes the application framework of MVC design pattern based on Java eeweb application, and it is a classic product in MVC Classic design pattern.

2.Spring MVC: Spring MVC is a follow-on product of springframework and has been integrated into spring Web flow. The Spring framework provides a full-featured MVC module for building WEB applications. Using spring's pluggable MVC architecture, you can choose to use Spring's SPRINGMVC framework or integrate with other MVC development frameworks, such as STRUTS1,STRUTS2, when using spring for web development.

3.Grails: Grails is an open-source framework for rapid Web application development, based on the groovy programming language and built on open source frameworks such as spring and Hibernate, a high-productivity, one-stop framework.

4.JSF: JavaServer Faces (JSF) is a standard framework for building Java WEB Applications (the JSR-127 standard defined by the Java Community Process). It provides a component-centric approach to building a user interface (UI) that simplifies the development of Java server-side applications. Driven by Java Community Process (JCP), it belongs to the technical specifications in Java EE 5 and is widely supported by vendors.

5.Tapestry: Tapestry is not a simple MVC framework, it is more like the combination of MVC Framework and template technology, it contains not only the front-end MVC framework, but also a view layer template technology, using tapestry can be completely separated from the Servlet/jsp API, is a very good design. By using tapestry, developers do not need to use JSP technology at all, users only need to use the template technology provided by tapestry, tapestry to achieve the complete separation of the view logic and business logic.

    

Implementation of the MVC pattern in Java Web applications

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.