Shang Silicon Valley @ModelAttribute analysis notes

The process of SpringMVC determining the POJO type of the target method into the parameters

  1. Determine a key:
    1). If the POJO type parameter of the target method is modified with @ModelAttribute, the key is the lowercase of the first letter of the POJO class name
    2). If @ModelAttribute is used to modify, the key is @ ModelAttribute annotated value attribute value.
  2. Find the object corresponding to the key in the implicitModel, and if it exists, pass it as an input parameter
    1). If it is saved in the Map in the method marked by @ModelAttribute and the key is consistent with the key determined by 1, it will be obtained.
  3. If the object corresponding to the key does not exist in the implicitModel, check whether the current Handler is decorated with the @SessionAttributes annotation.
    If this annotation is used and the value attribute value of the @SessionAttributes annotation contains the key, the key will be obtained from the HttpSession
    The corresponding value value, if it exists, is directly passed into the input parameter of the target method. If it does not exist, an exception will be thrown.
  4. If the Handler does not identify the @SessionAttributes annotation or the value value of the @SessionAttributes annotation does not contain a key, it
    will create a POJO type parameter through reflection and pass it in as the target method parameter
  5. SpringMVC will save the key and POJO type objects in the implicitModel, and then save it in the request.

Source code analysis process

  1. Call the @ModelAttribute annotation modification method. Actually put the data in the Map in the @ModelAttribute method in the implicitModel.
  2. Analyze the target parameter of the request processor. In fact, the target parameter comes from the target attribute of the
    WebDataBinder object. 1). Create a WebDataBinder object:
    ①. Determine the objectName attribute: If the incoming attrName attribute value is "", then objectName is the first A lowercase letter.
    *Note: attrName. If the POJO attribute of the target method is decorated with @ModelAttribute, the attrName value is the
    value attribute value of @ModelAttribute

②. Determine the target attribute:
> Find the attribute value corresponding to the attrName in the implicitModel. If it exists, ok
> if it does not exist: verify whether the current Handler is decorated with @SessionAttributes, if it is used, try to
get the attrName from the Session Corresponding attribute value. If there is no corresponding attribute value in the session, an exception is thrown.
> If the Handler is not decorated with @SessionAttributes, or the key specified by the value value in @SessionAttributes does not
match the attrName, it is created by reflection POJO object

2). SpringMVC assigns the request parameters of the form to the corresponding attributes of the WebDataBinder target.
3). SpringMVC will pass the attrName and target of the WebDataBinder to the implicitModel. The latter will be
passed to the request domain object.
4). The target of the WebDataBinder The input parameter passed as a parameter to the target method.

Guess you like

Origin blog.csdn.net/magicproblem/article/details/108477704