Implementation of the MVC pattern in Java Web applications

Source: Internet
Author: User
Tags java web

Introduction to MVC

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

MVC is a pattern of creating WEB applications using MVC (Model View Controller-View-controller) design.

Model (models) represent the core of the application (such as a list of database records). Is the part of the application that handles application data logic. Typically, model objects are responsible for accessing data in the database.

View displays data (database records). Is the part of the application that handles the display of data. Typically views are created from model data.

The controller (Director) processes the input (writes to the database record). Is the part of the application that handles user interaction. Usually the controller is responsible for reading data from the view, controlling user input, and sending data to the model.

MVC diagram:

The main components of a Java Web application

Servlet: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.

The ejb:enterprise Java Bean (EJB) component is a server-side Java component. This component is based on the standard distributed object technology, CORBA and RMI technologies, which are used to implement the amount of business logic for enterprise-level applications.

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.

The Javabean:javabean component is a Java object that conforms to a specific specification. A series of properties are defined in the JavaBean component, and each property provides a setter and getter method so that you can use the component to store some intermediate data. For example, ServletRequest request information, information from the database, and so on.

XML language: XML (extensible Markup Language) is a markup language used to create custom tags.

Java Web solution (development method)

  Jsp+servlet+javabean

Features: JSP as a view to the performance of the page, servlet as a controller, control program process and call the business processing; JavaBean encapsulates the business logic. Followed the MVC design pattern.

Cons: No unified development framework leads to long development cycles.

Jsp+javabean

Features: JSP as a view to the performance of the page, servlet as a controller, control program process and call the business processing; JavaBean encapsulates the business logic. Followed the MVC design pattern.

Cons: No unified development framework leads to long development cycles.

Struts

Features: A mature MVC development framework. Constitute:

Controller: Actionservlet Component: The central controller of the struts frame.
Requestprocessor components: Each submodule has a request processor.
Action component: A business agent that invokes the model for a specific business logic process.
Patterns: You typically use other model builds in structs to implement business logic. such as: JavaBean technology, EJB Technology, hibernates design mode.

Views: Mainly by the JSP page composition;
Also includes HTML document, standard tag library (JSTL) and Struts standard library;
JavaScript scripts and CSS styles;
multimedia files;
message resource files;
Actionform class.

Conclusion: For some large-scale projects, the struts framework will improve the development efficiency and have great benefit to the later maintenance.

Spring

Features: Based on two advanced technologies of IOC and AOP, the perfect simplification of the complexity of enterprise-level development is an ideal Web program framework.

Constitute:

Core module: Implement IOC mode, including Beanfactory class is responsible for the configuration and management of JavaBean.
Context module: inherits the Beanfactory class and adds functions such as event handling, internationalization, resource loading, transparent loading, and data validation.
Provides framework-style bean access and many enterprise-class features. such as: Jndi access, support EJB, remote call, inherit template framework, e-mail and scheduled task scheduling.

AOP module: Provides an AOP framework written in the standard Java language that allows applications to throw away the complexity of EJBS, but with the key functionality of traditional EJBS.
DAO Module: Provides an abstraction layer of JDBC and provides support for declarative and programmatic transactions.
Web module: Built on the context module, provides the context of the servlet listener and the contexts of the Web application. Integrates with existing web frameworks such as: JSF, Tapestry, struts, and more.
O/R Mapping module: provides support for existing ORM frameworks such as Hibernate

Spring MVC framework: Built on top of core modules, it adapts to multiple views, template technology, internationalization, and validation services, enabling clear separation of control logic and business logic.

Struts+hibernate

Features: Using Struts's MVC design pattern, a development program that consists of hibernate persistence objects.

Conclusion: For some large-scale projects, this method will improve the development efficiency and have great benefit to the later maintenance.

Struts+spring+hibernate

Features: Struts is responsible for the presentation layer,

Spring is responsible for the business of the logic layer,
Hibernate persistent Layer database operation, composed of development scenarios. Conclusion: For some large-scale projects, this method will improve the development efficiency and have great benefit to the later maintenance. Common Java Web MVC framework   Struts is an MVC framework based on the Sun EE platform, which is implemented mainly by servlet and JSP technology. Because struts can fully meet the needs of application development, easy-to-use, agile and rapid, over the past year has been a considerable concern.  Struts consolidates Servlets, JSPs, custom tags, and information resources into a single, unified framework that developers can use to develop without having to encode themselves to implement a full set of MVC patterns, greatly saving time. 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. Model 2 is based on the MVC (Model-view-controller, model-view-control) architecture pattern, typically using a small service (Servlet) or filter as the controller, which takes the user request and obtains the model data and jumps to the view The JSP page is used as a view to display the results of the user action, and the model is of course Pojo (Plain old Java object), which is a common Java objects that are different from the EJB (Enterprise JavaBean). Interfaces that do not implement any other framework do not play other roles, but are responsible for hosting the data and can be used as VO (Value Object) or as a DTO (Data Transfer object).

 

Implementation of the MVC pattern in Java Web applications

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.