SpringMVC source code parsing, springmvc source code

Source: Internet
Author: User

SpringMVC source code parsing, springmvc source code

ModelAndViewContainer is responsible for the transfer of Context data when HandlerAdapter processes requests.

The source code annotation is described as follows:

Records model and view related decisions made by HandlerMethodArgumentResolvers and HandlerMethodReturnValueHandlers during the course of invocation of a controller method.

Record the model and view information used by HandlerMethodArgumentResolver and HandlerMethodReturnValueHandler during handler processing.

ModelAndViewContainer:

1. Maintain the model, including defaultModle and redirectModel.

2. Maintain the view

3. Maintain redirect Information, and determine whether the HandlerAdapter uses defaultModel or redirectModel based on this judgment. (For details about the judgment rules, refer to the following section)

4. Maintain the @ SessionAttributes annotation information status

5. Maintain whether the handler processes the flag

 

Directory:

1. ModelAndViewContainer attributes

2. Popular Science ModelMap Inheritance System

3. The APIS provided by ModelAndView are complex attribute settings related to model. Others are simple getter and setter.

 

ModelAndViewContainer attributes

Let's take a look at the attributes of ModelAndViewContainer, which is clear:

1 package org. springframework. web. method. support; 2 public class ModelAndViewContainer {3 // view. The actual use may be a String-type logical view 4 private Object view; 5 // mark whether handler has completed request processing 6 private boolean requestHandled = false; 7 // default model, in the following example, we can use the ModelMap inheritance system 8 private final ModelMap defamodel model = new BindingAwareModelMap (); 9 // the model used in redirect. The actual model is RedirectAttributesModelMap10 private ModelMap redirectModel; 11 // flag the processor returns the redirect view 12 private boolean redirectModelScenario = false; 13 // when redirect, whether to ignore defamodelmodel14 private boolean ignoredefamodelmodelonredirect = false; 15 // @ SessionAttributes annotation using the status tag, is whether the processing is completed 16 private final SessionStatus sessionStatus = new SimpleSessionStatus (); 17}

Scenario solution for learning English words by the way

 

 

Popular Science ModelMap Inheritance System

Before proceeding to the analysis, let's take a look at the inheritance system of ModelMap:

Responsibilities and Use Cases of each category:

ModelMap is a subclass of LinkedHashMap. It mainly encapsulates the attribute concept, and the actual processing is still delegated to map.

ExtendedModelMap adds a chain to call chained CILS and implements the Model interface.

When BindingAwareModelMap and BindingResult attributes are set, the BindingResult. defaultModel class is automatically cleared.

RedirectAttributesModelMap converts the parameter type through DataBinder.

 

Focus on the source code.

2.1 ModelMap: inherits LinkedHashMap and adds mergeAttributes

 1 package org.springframework.ui; 2 public class ModelMap extends LinkedHashMap<String, Object> { 3     public ModelMap mergeAttributes(Map<String, ?> attributes) { 4         if (attributes != null) { 5             for (String key : attributes.keySet()) { 6                 if (!containsKey(key)) { 7                     put(key, attributes.get(key)); 8                 } 9             }10         }11         return this;12     }13     // ...14 }

2.2 ExtendedModelMap

1 package org. springframework. ui; 2 public class ExtendedModelMap extends ModelMap implements Model {3 @ Override4 public ExtendedModelMap addAttribute (String attributeName, Object attributeValue) {5 super. addAttribute (attributeName, attributeValue); 6Return this; // click here.7} 8 //... 9}

2.3 BindingAwareModelMap: When BindingResult attributes are set, the BindingResult. defaultModel class is automatically cleared.

RemoveBindingResultIfNecessary. Both put and putAll call removeBindingResultIfNecessary.

 1 package org.springframework.validation.support; 2 public class BindingAwareModelMap extends ExtendedModelMap { 3  4     @Override 5     public Object put(String key, Object value) { 6         removeBindingResultIfNecessary(key, value); 7         return super.put(key, value); 8     } 9     // ...10     private void removeBindingResultIfNecessary(Object key, Object value) {11         if (key instanceof String) {12             String attributeName = (String) key;13             if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {14                 String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;15                 BindingResult bindingResult = (BindingResult) get(bindingResultKey);16                 if (bindingResult != null && bindingResult.getTarget() != value) {17                     remove(bindingResultKey);18                 }19             }20         }21     }22 23 }

2.4 RedirectAttributesModelMap converts the parameter type through DataBinder. flash attributes used in redirect

In this way, Apis related to the dataBinder and flashAttributes attributes are added.

 1 package org.springframework.web.servlet.mvc.support; 2 public class RedirectAttributesModelMap extends ModelMap implements RedirectAttributes { 3  4     private final DataBinder dataBinder; 5  6     private final ModelMap flashAttributes = new ModelMap(); 7  8     private String formatValue(Object value) { 9         if (value == null) {10             return null;11         }12         return (dataBinder != null) ? dataBinder.convertIfNecessary(value, String.class) : value.toString();13     }14     // ...15 }

 

APIS provided by ModelAndView

There are two types of Apis: view and model.

3.1 view is actually an api, whether it is a class application (determined by whether it is a string type ):

 1 package org.springframework.web.method.support; 2 public class ModelAndViewContainer { 3     // ... 4     /** 5      * Whether the view is a view reference specified via a name to be 6      * resolved by the DispatcherServlet via a ViewResolver. 7      */ 8     public boolean isViewReference() { 9         return (this.view instanceof String);10     }11 }

3.2 there are a lot of model-related APIs, mainly to set model values.

When setting the model value, there is a problem here, that is, there are two models in the container. Which one should I set?

The container simply abstracts a getModel () api for judgment.

Let's talk about the logic of judgment:

Scenarios where defamodel model is used:

A, not redirect

B. In the redirect scenario, redirectModel is null and ignoredefamodelmodelonredirect is false.

The rest is the use of redirectModel.

The classes for model-related attribute operations are similar to addAttribute logic and are directly delegated after the model is obtained using getModel. For details, the following source code takes addAttribute as an example. The others only display the signature of the api.

1 package org. springframework. web. method. support; 2 public class ModelAndViewContainer {3 //... 4/** 5 * Return the model to use: the "default" or the "redirect" model. 6 * <p> The default model is used if {@ code "redirectModelScenario = false"} or 7 * if the redirect model is {@ code null} (I. e. it wasn't declared as a 8 * method argument) and {@ code ignoredefamodelmodelonredirect = false }. 9 */10 publ Ic ModelMap getModel () {11 if (useDefaultModel () {12 return this. defaultModel; 13} 14 else {15 return (this. redirectModel! = Null )? This. redirectModel: new ModelMap (); 16} 17} 18/** 19 * Whether to use the default model or the redirect model.20 * not redirect | during redirect, the redirectModel is empty and defaultModel21 */22 private boolean useDefaultModel is not ignored. () {23 return! This. redirectModelScenario | (this. redirectModel = null )&&! This. ignoreDefaultModelOnRedirect); 24} 25 public ModelAndViewContainer addattriiner (String name, Object value) {26GetModel (). Addattriiner (name, value); 27 return this; 28} 29 public ModelAndViewContainer addattriiner (Object value) {}; 30 public ModelAndViewContainer addAllAttributes (Map <String,?> Attributes) {}; 31 public ModelAndViewContainer removeAttributes (Map <String,?> Attributes) {}; 32 public boolean containsAttribute (String name) {}; 33}

 

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.