MVC Development Model

Source: Internet
Author: User

A brief introduction to MVC

The architecture of the Java Web application has undergone two development patterns, Model1 and Model2, respectively. Model1 is composed of a large number of JSP pages and a small number of JavaBean, the page and Java code together regardless of the development, extension or maintenance have great inconvenience, so based on the MVC development model Model2 came into being.

MVC is a development model that decouples our code and allows the view code to be written separately from our logic code, which is a great convenience for our later maintenance. MVC divides our project structure into three partial view layers (views), control layers (controllers), model layers (models), view-specific display, that is, we can see the page in an application, model full-time and database additions and deletions to change the operation, The controller is responsible for connecting the view with the controller, which is a bridge between the two.

Shows how the MVC development model works:

MVC schematic

The request that we generate on the page (View) (click on a hyperlink to deliver a location or a SRC request for a picture) is handled by the Controller (Controller), which operates according to the user's request. If the interaction of the data is required to invoke the model layer method to complete the interaction of the data, the model layer mainly deals with the deletion of the data. This set of processes ran through our request and response.

If we now have a user-registered function to implement.

The view layer is as follows:

<formMethod= "POST"Action= "Demo!register.action">User name<inputtype= "text"name= "username"><BR/>Password<inputtype= "text"name= "Password"><BR/>Email<inputtype= "text"name= "Email"><BR/>    <inputtype= "Submit"value= "Register"></form>

The controller layer is as follows:

 private   String username;  private   String password;  private   String email;  public     String Register () throws   exception{ Handlerservice service  = new      Handlerservice ();  if   (service. Handlerregister (username, password, email)) { return  "Success" ;  else  { return  "fail" ; }}

The model layer is as follows:

 Public Boolean handlerregister (String username,string password,string email) {    try  {        /*          * Perform database operation        here *         /System.out.println (username);        SYSTEM.OUT.PRINTLN (password);        SYSTEM.OUT.PRINTLN (email);         return true ;     Catch (Exception e) {        returnfalse;    }}

Now we focus on the processing process, the implementation of the above code is ignored.

First we create a request on the page: demo!register.action

This request is handled by the controller: Gets the information entered by the user, and then passes the model layer to the model layer method as an argument to complete the operation of the user's input data, and then feeds the results back to the user

The model layer mainly realizes the logic of data processing, we can consider the model layer method as our usual encapsulation method, reserve the parameters, and then this method is called by the controller.

(the page that the above request demo!register.action and controller returns "Success" and "fail" is controlled by struts2)

MVC Development Model

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.