On the three frames of Javaweb and the MVC design pattern

Source: Internet
Author: User

First, the MVC design pattern

Concept of 1.MVC

First we need to know that the MVC pattern is not unique to the Javaweb project, MVC is a software architecture model in software engineering that divides software systems into three basic parts: model, view, and controller, which is MVC. It is a model of software design, the first proposed for Trygve Reenskaug, for the Xerox Palo Alto Research Center (Xerox PARC) Smalltalk language invented a software design model.

2.MVC explanation

Although MVC is not unique in Java, but now almost all of the B/S architecture has adopted the MVC framework model, but MVC in the B/s architecture is not fully implemented, in fact, we do not need to master the unfinished part.
Controller controllers: The controller is the processing logic of the control request, processing the request, responsible for forwarding the request;
• Views View: The view is the interface that the user sees and interacts with, such as HTML (static resources), JSP (dynamic Resource), and so on.
• Model: The model represents an enterprise specification, which is the processing of business processes/statuses and rules of business. The processing of business processes is opaque to other tiers, the model accepts requests for view data, and returns the final processing results. The design of the business model can be said to be the core of MVC.

Where 3.MVC mode comes in

The MVC pattern is widely used in various frameworks of Java, such as Struts2,Spring MVC, and so on.

STRUTS2 is a lightweight Web application framework based on MVC. Based on MVC, it is explained that Web applications based on Struts2 are naturally able to implement MVC, and that Struts2 focus on the various parts of MVC to help us with our development.

Second, Javaweb Classic three-tier architecture

1.Javaweb has gone through three of times

①jsp Model1 First generation
JSP Model1 is the early model of Javaweb, it is suitable for small web projects, low development cost! Model1 first generation period, the server side only JSP pages, all operations are in the JSP page, even the API to access the database is also done in the JSP page. That is to say, all things are coupled together and are extremely detrimental to later maintenance and expansion.

②jsp Model1 Second generation
JSP Model1 second generation has improved, the content of business logic into the JavaBean, and JSP page is responsible for display and request scheduling work. Although the second generation better than the first generation, but also let JSP do too much work, JSP in the view work and request scheduling (Controller) work coupled together.

③jsp Model2
JSP Model2 mode has been able to clearly see the full structure of MVC.
· JSP: The view layer, used to deal with users. Responsible for receiving the data used and displaying the data to the user;
· Servlet: The control layer, is responsible for finding the appropriate model object to handle the business logic, forwarded to the appropriate view;
JavaBean: The model layer, complete the specific business work and logic, such as: open, transfer and so on.

This is the Javaweb experience of the three years, JSP Model2 suitable for many people to cooperate in the development of large-scale web projects, their duties, non-interference, conducive to the development of division of labor, conducive to the reuse of components. However, the development of Web projects is more difficult, and the technical requirements of developers are also improved.

2.JAVAWEB Classic three-layer frame

We often say that the three-layer framework is proposed by Javaweb, which means that this is javaweb unique!
The so-called three tiers are the presentation layer (the Web layer), the business logic layer, and the data access layer.
· Web layer: Contains Web-related content such as JSP and servlet;
• Business layer: The business layer does not contain the Javaweb API, it only cares about the business logic;
• Data layer: Encapsulates the access details of the database;

Note that the Javaweb API cannot appear in the business layer, such as request, response, and so on. In other words, the business layer code is reusable and can even be applied to non-web environments. Each method of the business layer can be understood as a panacea, such as a transfer business method. The business layer relies on the data tier, and the Web tier relies on the business layer!

Note: Many people on the internet are talking about whether MVC is a framework or a design pattern (the Baidu encyclopedia forcefully interprets it as a framework rather than a design pattern), although there is no MVC in the 23 design patterns in Java, I don't think it's much of a problem to discuss it. We can think of him as a thought, all we need to do is to understand it, to apply it, to master the ideas, that's what we need to do.

Struts2, Spring2, hibernate3 in ssh in what role

To put it simply:

For Struts control

Hibernate operations database.

Spring uses a decoupled
To say in detail:

STRUTS in the SSH framework to play the role of control, the core is the controller, that is, Actionservlet, and the core of Actionservlet is Struts-confi g.xml. The main control of the processing of logical relationships.

Hibernate is a data persistence layer, a new mapping tool for objects and relationships, provides a mapping from Java classes to data tables, and provides mechanisms for querying and recovering data, which greatly reduces the complexity of data access. The direct operation of the database is converted to the operation of the persisted object.

SPRING is a lightweight control inversion (IoC) and facet-oriented (AOP) container framework, interface-oriented programming, and (dependency) relationships between container control programs, rather than traditional implementations, directly manipulated by program code. This is the concept of the so-called "inversion of Control": (dependency) control is transferred from the application code to the external container, and the transfer of control is called inversion. Dependency injection, in which the dependencies between components are determined by the container at run time, the image is that the container dynamically injects some kind of dependency into the component
The main function is to decouple


The role of Struts, spring and Hibernate in each layer

1) Struts is responsible for the Web tier.

Actionformbean receives the data submitted by the form in the Web page, then processes it through the Action and Forward it to the corresponding page.

The definition of <action-mapping>,actionservlet in Struts-config.xml is loaded.

2) Spring is responsible for business layer management, i.e. service (or manager).

1. The service provides a statistical calling interface for the action, encapsulating the DAO for the persistence layer.

2. Can write some of their own business methods.

3. A unified approach to JavaBean management

4. Declarative transaction Management

5. Integrated Hiberante

3) Hiberante, responsible for persistence layer, complete the database crud operation

Hibernate is a persistent layer that provides or/mapping.

It has a set of. hbm.xml files and POJO that correspond to the tables in the database. Then you define DAO, which is the class that deals with the database, and they use the PO.

In the Struts+spring+hibernate system,

The calling process for an object is: Jsp-> Action-> Service->dao->hibernate.

The flow of data is Actionformbean accepts the user's data, the Action takes the data out of the Actionfrombean, encapsulates it into VO or PO,

Then call the business layer of the Bean class, complete a variety of business processing and then forward. When the business layer Bean receives the PO object, it invokes the DAO interface method for persistence.

On the three frames of Javaweb and the MVC design pattern

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.