The MVC design pattern of Javaweb

Source: Internet
Author: User

Time: 2016-11-26-16:22

--mvc





The MVC pattern (model-view-controller) is an architectural pattern in software engineering, which divides the software system into three basic parts:
Model, view, Director (Controller)

The MVC pattern was first proposed by Trygve Reenskaug, a software design pattern invented by the Smalltalk language of Xerox Palo Alto Research Center (Xerox PARC).

MVC provides convenience for later maintenance and extension of programs, and facilitates the reuse of parts of the program, and MVC simplifies the program and makes it more intuitive.

Controller:
Servlet
The request is processed and the request is forwarded.
Views (view):
Jsp
Interface designer for graphical interface design.
Model:
JavaBean
Write program application functions (Implementation algorithm, etc.), database management.

MVC is not unique to Java, almost all of the B/s structure of the software has adopted the MVC design pattern, but to note that MVC in the B/s structure software is not fully implemented, for example, in the future B/s software will not be event-driven.




--javaweb and MVC

Javaweb experienced JSP Model1 generation, JSP Model1 second generation, JSP Model2 three times.
Java classes with specific functionality, located at the model level. (PO bag, DAO package, BL Package ' Service Pack ')

1, JSP Model1 first generation
JSP Model1 is the early model of Javaweb, which is suitable for small web projects with low development costs.
JSP Model1 first generation period, the server side only JSP page, all the operation in the JSP page, even access the database API is also done in the JSP page, that is, all things are coupled together, the maintenance and expansion of the later is extremely unfavorable.




2, 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 is better than the first generation, but JSP still do too much work, The JSP is coupled with the work of the view work and request scheduling (Controller).




3. JSP Model2
JSP Model2 mode has been able to clearly see the full structure of MVC.

1) JSP: The view layer, used to deal with the user, is responsible for receiving the data to be processed, as well as displaying the data to the user.
2) Servlet: Control layer: Responsible for finding the right business model object to handle business logic and forwarding to the appropriate division view.
3) JavaBean: Model layer, complete the specific business work, such as: Account opening, transfer and so on.

JSP Model2 is suitable for many people to cooperate in the development of large-scale web projects, their responsibilities, non-interference, conducive to the development of division of labor, is conducive to the reuse of components, but the development of Web projects more difficult, but also the technical requirements of developers higher.

Structure diagram:





--javaweb three-tier architecture

We often say that the three-tier architecture was proposed by Javaweb, which means that this is javaweb unique.
The so-called three-tier architecture is the view layer (the Web layer), the business logic layer, and the data access layer.

* Web tier:
Servlet, JSP
Contains web-related content such as JSP and servlet, such as request, Response, Session, ServletContext.
* Business layer:
Service
For example, login, register.
The business layer does not contain the Javaweb API, it only cares about business logic.
Because if the Web API is included, the business layer cannot be used in C/s.
Once the business logic is hooked up to the Web tier, the logical implementation can only be done at the Web tier.
* Data layer: (persistent layer)
DAO layer, Database Access Object.
Encapsulates the details of access to the database, such as additions and deletions to the database.
All operations on the database cannot jump out of DAO, because once the database is replaced, it involves code such as the business layer.

* Entity class:
JavaBean

It should be noted that in the business layer can not appear javaweb API, such as request, response, that is, the business layer code is reusable, and even can be used in a non-web environment, the business layer of each method can be understood as a universal, such as the transfer of business methods, The business layer relies on the data tier, and the Web tier relies on the business layer.


Layered Architecture:
Based on MVC, JavaBean is divided into business layer and data layer.





--hello World



1, Com.wyc.dao

The public class Userdao {/* * * queries the data in the database, encapsulates it into the user object, and then returns.        */Public User find () {return new User ("Zhangsan", "123"); }    }

2, Com.wyc.domain
/** * Save the results of the query in the database to this object
* @author 31067 * * */public class User {private String username; private String password;
....
}

3, Com.wyc.service
public class UserService {//service layer relies on DAO layer private Userdao Userdao = new Userdao ();        Service layer queries, which need to be done using DAO.        Public User Find () {return userdao.find (); }    }
4, Com.wyc.web.servlet
    public class Userservlet extends HttpServlet {         public void doget (HttpServletRequest request, httpservletresponse response)  throws servletexception, IOException {            /*              * relies on the service in the servlet and then saves the results to the request through the service completion function. Then forward to the JSP page for display              */             userservice userservice = new UserService ();             user User = Userservice.find ();              request.setattribute ("user", user);             //Request forwarding can only use relative paths               request.getrequestdispatcher ("show.jsp"). Forward (request, response);             system.out.println (Request.getcontextpath () + "/show.jsp") ;         }    }

5, index.jsp
<body> <a href= "${pagecontext.request.contextpath}/userservlet" >dianji</a></body>



6, show.jsp

<body> name: ${user.username} password: ${user.password}</body>


Pop frame: November 26, 2016 16:21:32
Ssh:
Struts2 + Spring + Hibernate

Springmvc

Ssm

The MVC design pattern of Javaweb

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.