Spring Learning 10-SPRINGMVC Principles and core components 1

Source: Internet
Author: User

first, the principle of SPRINGMVC
when the request arrives, the first front-end controller to accept the request is called Dispatcherservlet ( which needs to be configured in Web. XML ), and the backend controller is called controllers. Called Handmapping, which handles the request URL and back-end controller mappings, has several types, is flexible, and is configured on an XML file. The model object that is responsible for business logic processing is usually the dao/dto component that we usually write. Just its last return is more flexible, the controller returns a Modelandview object to Dispatcherservlet,modelandview can carry a view object, or it can carry the logical name of a View object. If you are carrying the logical name of a View object, then Dispatcherservlet needs a viewresolver to find the view object used to render the response. Finally, Dispatcherservlet assigns the request to the view object specified by the Modelandview object. The View object is responsible for rendering the response returned to the customer.
The more specific workflow is:
① when a user taps a link in a browser or submits a form, a request is generated. When the request leaves the browser, it carries the information requested by the user (such as the requested URL information, user name, password, etc.).

The first stop of the ② request arrives at Spring's Dispatcherservlet, which is a front-end controller that is working to delegate the user's request to other components (which are handed to SPRINGMVC's controller) for processing. Here Dispatcherservlet to decide which controller to pass the request to, then the processor mapping (handlermapping) is required. The processor map looks at the requested URL information and then decides which controller to handle the request. For example, there are two controllers Controllera and Controllerb, Processing requests from the suffix named. html and. JSP, and then when the requestor's suffix is. html, the Dispatcherservlet will send the request to Controllera for processing.

③ when a suitable controller is selected, the Dispatcherservlet will give the request to the controller to handle. On this controller, the user's request will be submitted by the user to the controller processing and waiting. However, the design of the better controller itself to do little or no processing of information, but the business logic to one or more server objects (Model) to deal with.

④ when the controller processes the information that the user requests carry (or gives it to the model layer), it often produces some other data that needs to be returned to the browser for display. The raw data is clearly unfriendly, and the view is needed to display the data. The last thing the controller does is package the model data and specify a specific name for the view that produces the output, and then it sends the model, view name, and request requests to dispatcherservlet. So the controller is not coupled to the view, because the name of the view passed to Dispatcherservlet is not a particular file name specified (such as the suffix name must be a JSP or something else), as long as it is a logical name that produces output and shows the result.

⑤dispatcherservlet requests a View parser (Viewresolver), which maps the logical view name to a specific view display file.

⑥ now Dispatcherservlet know which view file can display the results. The view will produce output using the template data, which is returned to the client for display through the response object.
About for the process of request processing in Dispatcherservlet, see:


    Second, SPRINGMVC core interface (critical)
     1, handlermapping interface--mapping of processing requests

Spring provides three useful handler Mapping implementation:

     --beannameurlhandlermapping
           maps a controller to a URL
  based on the name of the controller;

For example: This is configured in the XML of the mapping file:




--simpleurlhandlermapping
to map a controller to a URL with a collection of properties defined in the context configuration file

For example: This is configured in the XML of the mapping file:



--commonspathmaphandlermapping
To Map a controller to a URL by using metadata in the controller code


For example: This is configured in the XML of the mapping file:




2, CONTROLLER interface--one of the back-end controller

Controller inheritance system such as:



thatLet's take a look at the Modelandview function first.:
    2.1: General backend Controller Abstractcontroller abstract class implementation Class example:
public class Hellocontrollerextends Abstractcontroller {

Publicmodelandview HandleRequest (HttpServletRequest request,

HttpServletResponse response) throws Exception {

Request.setattribute ("Hello", "Welcome to spring!");

return new Modelandview ("Welcome");

}
}
  
2.2: commandcontrollers ( command back-end controller )
  

The implementation classes of these controllers include: Abstractcommandcontroller abstract class, Abstractwizardformcontroller abstract class, Simpleformcontroller class, and so on.


2.3: Other Controllers (other back-end controllers)

 



3, Throwawaycontroller interface--Two back-end controller
There is no class in the SPRINGMVC jar package that implements it:


Example:
A:throwawaycontroller class:
public class Examplethrowawaycontroller Implementsthrowawaycontroller
{
Privatestring msg;
Public voidsetmsg (String msg)
{
This.msg =msg;
}
@Override
Publicmodelandview execute () throws Exception
{
stringhashcodemsg = "[" +hashcode () + "]-" +msg;
Return Newmodelandview ("throwaway", "MSG", hashcodemsg);
}
}
B: The [servletname]-servlet.xml] of the mapping file should be configured:
<beanid= "Examplethrowawaycontroller" class= "Com.huateng.ExampleThrowAwayController" scope= "prototype" >

4, Handlerinterceptor interface--Interceptor
Inheritance System:



Example:









Spring Learning 10-SPRINGMVC Principles and core components 1

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.