MVC is the abbreviation of three words, which are: models, views (View), and Controller. The purpose of the MVC mode is to achieve the functional division of Web systems. The following is an introduction to J2EE development.
The MODEL layer realizes business logic in the system, usually can be implemented with JavaBean or EJB.
The View layer is used to interact with the user, usually implemented with JSP.
The Controller layer is a bridge that the Model communicates with the View, which can assign users to request and select the appropriate view for display, and it can also explain the user's input and map them to the model layer can be performed.
Now let's take an example to see how the MVC mode works.
1.1.1 an instance
Example 1-A:
controller servlet-name>
nepalon.simplestruts.controller servlet-class>
servlet>
controller servlet-name>
/ simplestruts / servlet / control / controller url-pattern>
servlet-maping>
The above is a clip of the web.xml file, where a servlet is defined here to process the request.
In the servlet, there is no direct processing request, but the processed process is pushed into the ArticleCommand class, executed by ArticleCommand object, such as code (1). After processing the request, go to the corresponding page, such as code (2). Below, let's take a look at the code of the ArticleCommand class.
Example 1-D:
Package nepalon.simplesplestruts;
/ **
*
Title: MVC Framework P>
*
Description: Article business class
*
Copyright: R2003 P>
* @Author nepalon
* @Version 1.0
* /
Import java.util. *;
Import javax.servlet. *;
Import java.io. *;
Import java.lang. *;
Import java.sql. *;
Import javax.sql. *;
Public Class Contribute
{
Public control () {}
Public String getAllarticle (httpservletRequest request, httpservletResponse response)
In this class, it is to obtain the business of all articles, and finally returns the page to jump to the page if the operation is successfully executed. Of course, there may be a corresponding function of other services in this class, readers can be implemented. Let's take a look at the code you want to jump to.
In this JSP, we have to do just the results and display, and there is no corresponding business logic.
1.1.2 instance analysis
First, let's take a look at the sequence diagram of this example.
Figure 1.1.2-1
1) First submit a request / SimpleStruts / Servlet / Control / Controller? Command = ShowArticle;
2) In the controller object of the Controller layer, the corresponding service processing class is called according to the type of request. Here, the business handler of the COMMAND value is the service processing class for the ShowArticle request class, so the corresponding function of the object is called;
3) In the MODEL layer's ArticleCommand class, the request to obtain the business function of all articles, save the results in the request, and return the jump page as the return value; 4) Back to the Controller layer of the Controller layer, according to the previous steps The return value is page forward.
5) Forward to the showallarticle.jsp page of the View layer, this page acquires results from the request and display. Although there is also a JAVA code in this JSP, these codes are just used to display the results and do not involve any business logic.
1.1.3 Benefits of MVC mode
This simple feature we only need 2 JSP files to be implemented. Why should you be so troublesome to achieve it in several classes? Take a look at the benefits of MVC mode.
1) Each is a matter, and it does not interfere.
In the MVC mode, each of the three layers is applied, so if the demand of which layer has changed, only the code in the corresponding layer is needed without affecting the code in the other layer. If the business changes, if WebMaster may have invalid sign when you take a article, you can't take all articles, you can only take out a valid article, and the business changes. Imagine if this business logic is used in 100 pages, then the MVC mode reflects its flexibility. We can do not change any JSP, just change the SQL statement in the corresponding class in the Model layer.
2) Help the division of labor in the development
In MVC mode, the division of labor can be better implemented because of the lamination of the system. Web designers can develop JSP in the development view layer to develop business layers for business familiar developers, while other developers can develop control.
3) Contribute to the reuse of components
After the layering, it is more conducive to the reuse of the components. If the control layer can be used independently, the vision layer can also be a universal operating interface.