Java EE enterprise-level application development series 2: MVC and three-tier architecture, javaeemvc

Source: Internet
Author: User

Java EE enterprise-level application development series 2: MVC and three-tier architecture, javaeemvc

Warning from Shanghai shangxuexiao School: confidence, effort, and success; confidence, effort, and success.

Step 1. Scenario overview

Hello, friends, I shared with you yesterday the concept that everyone is familiar with in JAVA EE Enterprise Application Development-layer-3 architecture, so some friends may have doubts, this code writing method is used every day when I write these web projects. What is the difference between it and the MVC we are exposed, today, we will talk about the MVC that our programmers often mention during development.

Step 2. Problem Analysis

During development, we may inadvertently confuse the three-tier architecture with Mvc, but they are not a concept. Next I will reveal some of my "truth ".
Three-tier architecture: Generally, the three-tier architecture divides the entire Business application into: User Interface layer and Business Logic Layer) and Data access layer ). The purpose of hierarchy differentiation is the idea of "high cohesion and low coupling.
MVC: The full name is Model View Controller, short for Model-View-Controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic.
Haha, the concept is a bit dizzy. How can I understand it?

Step 3. Regression code

In step 2, I explained the three-tier architecture and the concept of MVC. Of course, for our programmers, the concept is a cloud, and only code is our favorite, next, let's take a look at how to understand the difference between the two through code. We also used yesterday's user login scenario for analysis.
Without layering and MVC, we want to respond to a login page from the browser through the server. For java programmers, the code we see will be in the following form:




The specific login code is first handled as follows:

The code we can see will be as follows:



Parameter retrieval and non-empty processing:
Jdbc operation query user records:



The logon result is returned to the client:



Which of the following code is a magic horse? It seems that we have returned to the "primitive age "...
 
The above code has actually implemented a simple user login function, but there is still a big gap with the code we usually write. What is the specific gap.
After analyzing the above Code, we did not adopt a three-tier architecture to decouple the code. The problem is that the servlet code is rather bloated, without using our three-tier architecture to layer the code, the processed code will be as follows:
Dal (Data Access Layer) Data Access Layer is what we usually call Dao Layer:




The Bll (Business Logic Layer) Business Logic processing Layer is what we usually call the Service Layer:

The Ui (User Interface layer) Interface layer is the jsp + servlet we use here.



Logon page:



Logon successful welcome page:



Now, a simple login function code has been reconstructed, and the login code looks similar to the functional code we usually write, there may be no enterprise project in terms of business processing, so there are many businesses. Here we take the simplest example and understand it. Haha, other business code is just a float for our programmers.
 

S Tep 4 Code analysis and summary

Through the code analysis above, we can see that the login function implements both layer-3 architecture processing and MVC shadow. How can we further understand the relationship? haha, now, let's make a simple look:
The three-tier architecture divides the entire project into: presentation layer (UI), business logic layer (BLL), and data access layer (DAL ).
MVC is Model, View, and Controller ).


The specific relationship diagram is as follows:

1. Both are "Three Layers ".
Although they are all "Three Layers" (not necessarily three layers, but also multiple layers), they are divided differently. You can see the difference from the figure above.
2. MVC is always accompanied by a three-tier architecture, but the three-tier architecture is not necessarily accompanied by Mvc
Although they are all three layers, the two focuses are different. The three-layer architecture focuses on "High Cohesion and low coupling", that is, decoupling. The purpose of MVC is to achieve the functional division of the Web system, that is, the division of duties. Layer 3 is based on business logic, while mvc is based on pages. MVC is mainly used for the presentation layer, and the three layers are mainly used for the system architecture. The three layers are generally the presentation layer, middle layer, and data layer, where the presentation layer can also be divided into M, V, C (Model View Controller) model-View-controller. For a three-tier architecture, not only web projects are used, but other forms of projects are also applicable.

Haha, I believe you have a deep understanding of the three-tier architecture and Mvc. Well, today's sharing will come first, and we will continue to talk about it tomorrow. You are welcome to continue to pay attention to Shanghai shangxue java.

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.