MVC Design Pattern Javaweb implementation

Source: Internet
Author: User

JSP development Modeldevelopment of JSP development model1. Mode 1: (Development of technology suitable for small projects)A. The first version number. Pure JSP (encapsulates data. Process data, display data)B. Second version number, Jsp+javabean.JSP: Collect data, display dataJavaBean: encapsulation, processing2. Mode 2:servlet+jsp+javabean (is the detailed implementation of MVC in Java, which is the detailed content of Java technology Implementation)A.servlet: Responsible for coordinating JSP and JavaBean, obtaining data, processing data (business logic), encapsulating into JavaBean, choosing JSP to display data. b.jsp: DisplayC.javabean: Encapsulation and simple handlingthought: Business processing is separated from display data.
Software Design Patterns1.MVC Software design pattern (not related to Java)is a design method for separating the business logic and the display interface. M:model ModelV:view ViewC:controller Controller

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahlta2luzw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">2. For the MVC pattern developed by Javaweb:
servlet (Controller) +jsp (view) +javabean (model)
so the classic three-book architecture of the system diagram is as follows: After reaching such a classic three-tier architecture. The architecture of Javaweb has been very good. However, from the perspective of software project, the above architecture still has problems.
so look at the use of the above architecture.
example:
web Layer: Userservlet.java class
service Layer: UserService class
dao Layer: Userdao class
JAVABEAN:USER
So the call relationship between layers is like this:
call UserService in Userservlet.java.


For example, the following:
Class userservlet{
Userservcie userservice=new UserService ();
Invoke UserService's related methods for business logic operations

。。


}
Similarly: When UserService calls Userdao, there are similar codes.
The above analysis concludes 1:
1. The layer-to-layer relationship between the above code is very close.


Is contrary to the design idea of software project.
Software project requires that the lower the coupling between modules and modules, the better. A classic software project design is the words of this.
"High cohesion. Low coupling ".
At the same time as the DAO layer above, let's look at both the operation of XML and the operation of DB, and even many others.

Reached the conclusion 2:
2. The above architecture. At the same time against the "high cohesion." Low coupling "in the thought of high cohesion.


As a result, the above architecture system has the following enhancement: For example, the above architecture diagram is analyzed:
Look at the service layer. DAO layer
1. Solve the problem of cohesion in the DAO layer and separate the Xmldao and Dbdao.
2. Extract a common interface to the DAO layer. DAO interface
Analysis: In the previous schema, the service layer called the DAO layer is this.


UserService need to write this code: Xmldao xmldao=new Xmldao ()//There is no interface at this time
After adding the interface, Dao dao=new Xmldao ();
3. Join the Factory
The above wording becomes: Dao Dao=factory.getxmldao ();
So how does the factory invoke the DAO layer related methods? Assuming a direct call, the result is that while the coupling between the service layer and the DAO layer has been conquered, it is actually just evolving
into a high coupling between the factory and the DAO layer.


Then the XML technology is added, and the factory reads the XML file. Then, using the reflection of Java, it is OK to create the object you want to create.
In XML, however, only the full path to the class is provided. The configuration is then made to allow the factory to read the XML file to create the object.
4. Join the configuration file.


Configuration file But string. There is no coupling relationship between the DAO layers.
Summary: For the third architecture (high cohesion. Low coupling) for easy maintenance and easy expansion.
Why it's easy to extend, for example: The DAO layer. With the new persistence technology, the service layer actually knows nothing. Just change the configuration file.

The factory will go to have
The corresponding operation. This is not easy to maintain and extend.

Up there. We looked at the service layer. The DAO layer realizes decoupling by means of interfaces, factory classes, and configuration files.
Similarly, the web layer, the service layer, can be decoupled in the same way.

is not fully reflected in the.

With the development of technology. There have been a lot of MVC frameworks, such as STRUTS+SPRING+HIBERNATE/SPRINGMVC.
When these frameworks are just emerging, they are basically decoupled from the above-mentioned approach.

People who have used the framework know that frameworks are based on configuration files, reflection principles, introspection, etc.
The people who have developed the structure now, think. Writing configuration files is also very laborious. There is now a framework design based on the annotation pattern.
But. The above architectural ideas, and evolution process, is the source of late framework technology OH.
Later, I will also do a more in-depth analysis of the framework technology of the Javaweb project development process in conjunction with the MVC design pattern.

Usually the development of javaweb+ framework technology is called
For Java enterprise-level development.

It is also a popular j2ee/javaee.
Add: In the Javaweb project based on the MVC software design pattern, typically the following subcontracting structure is performed:

Use of subcontracting to describe narrative structure

Com.ghsy, company domain name Inverted (Anhui anqing Gaoheng Plastic Industry Co., Ltd.)

com. Ghsy . Project Name

com. Ghsy . Project name. Dao DAO Interface

com. Ghsy . Project name. Dao.impl DAO Implementation Class

com. Ghsy . Project name. Service Service Interface

com. Ghsy . Project name. Service.impl Service Implementation Class

com. Ghsy . Project name. Web.servlet servlet Processing Class

com. Ghsy . Project name. Web.filter Filter Processing class

com. Ghsy . Project name. Web.listener Listener Processing Class

com. Ghsy . Project name. Domain JavaBean encapsulated data (bean)

com. Ghsy . Project name. Utils Tool Class

com. Ghsy . Project name. Exception Define Exceptions yourself

com. Ghsy . Project name. Constant Java constant Xxx.name

/web-inf/jsp page, security

* Make a request to forward a display JSP page


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

MVC Design Pattern Javaweb implementation

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.