Implementation principle of request-oriented MVC Framework

Source: Internet
Author: User

1) simple process: the Web Client sends an HTTP request to the Web server (Tomcat). The web server analyzes the request and is responsible for finding the servlet for the request. Process the request in servlet. To process a request, you can use HTTP request to obtain the data in the form, call the business logic, and process the data in the methods in the business logic. After processing, the corresponding results are returned to the servlet, and set the results to the request. The servlet is also responsible for turning to the view (JSP), first to the Web server, and the Web server is responsible for finding the corresponding view. In the view, get the corresponding data, the data has been placed in the request, get the data, generate a response to the Web server, the most web server to the browser that is JSP page.

2) layering. The most important thing is to separate data and display. MVC is a layered idea. M (model): corresponds to the business logic layer (action); V (view, view): corresponds to the JSP page, responsible for display; C (controler, Controller ): corresponding to servlet, responsible for obtaining form data, calling business logic, and turning. MVC intercepts all requests through a class (usually servlet), calls the business logic for processing, and returns the results to this class after the processing is complete, this class is then responsible for turning to the display page. MVC can be implemented in two ways: Request-oriented (struts, webwork) and event-oriented (JSF ). MVC was proposed at the end of 1980s.

3) detailed process: Request-oriented MVC has a controller (servlet) called the central controller, which is responsible for intercepting the requests submitted on the page. The interception method is as follows:

Servlet configuration is on the web. in XML, servlet can intercept URL requests through <URL-pattern>/servlet/testservlet </url-pattern>, the configuration such as/servlet/testservlet can only intercept this URL. To intercept all URLs, you can modify the matching mode here. For example, you can change it to *. Do (this can be selected as long as it matches), so that all. Do requests can be intercepted. This servlet intercepts all action = "*. Do" requests in the form on the front-end web page. For example, our action = "adduser. Do" is intercepted by the servlet. The interception method is as follows:

String currenturi = request. getrequesturi (); // obtain all the current Uris
System. Out. println (currenturi); // print the current URI
String Path = currenturi. substring (currenturi. indexof ("/", 1); // capture *. Do

System. Out. println (PATH); // print the current URI
Path = path. substring (0, path. indexof ("."); // capture *
System. Out. println (PATH); // print the current URI
The output result is:

/Test_servlet/user/adduser. Do

/User/adduser. Do
/User/adduser

The final "*" is what we need. The business logic layer calls the function based on this decision for processing.

After interception, it is distributed to the corresponding action. The action class is a class that implements a unified interface and overwrites its execute method, in the method, different functions are implemented based on different URLs.

Servlet will find the corresponding processing Class Based on the configuration file. The configuration method is as follows:

<Action-config>

<Action Path = "/user/adduser" class = "the detailed address of this action, for example, Com. liu. struts. useraddaction "Success =" add_success.jsp "/> // success ="/add_success.jsp "indicates the page to which the user is redirected after successful addition.

</Action-config>

In this way, after the request is intercepted, find the corresponding action in the configuration file and process it. When there is a new business, you only need to write the action and configure it in the configuration file.

 

 

4) struts Implementation of MVC:

When a request arrives, it reaches the actionservlet and finds the corresponding action through the struts-config.xml. At the same time, it puts all the data in the form into the actionform. when the action is called, it passes the actionform, call the model layer in the action for processing. After the call, return an actionforward object and return it to the actionservlet. The actionservlet parses the actionforward object and completes the redirection based on the information in actionforward.

 

 

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.