SpringMVC 10 ModelAttribute source code analysis, running process, and confirming the process of entering parameters of the target method POJO type

Source code analysis process

  1. Call the method modified by the @ModelAttribute annotation. In fact, the data in the Map in the @ModelAttribute method is placed in the implicitModel.
  2. Parse the target parameter of the request processor, in fact, the target parameter comes from the target attribute of the WebDataBinder object
    1). Create the WebDataBinder object:
    ①. Determine the objectName attribute: If the incoming attrName attribute value is "", then the objectName is the class name A lowercase letter.
    Note: attrName. If the POJO attribute of the target method is modified with @ModelAttribute, the value of attrName is the value of the @ModelAttribute
    ②. Determine the target attribute:

    • Find the attribute value corresponding to attrName in implicitModel. If it exists, ok, if it does not exist: verify whether the current Handler is modified with @SessionAttributes, and if so, try to retrieve it from the Session
    • Get the attribute value corresponding to attrName. If there is no corresponding attribute value in the session, an exception will be thrown.
    • If the Handler is not modified with @SessionAttributes, or the key specified by the value value in @SessionAttributes does not match the attrName, the POJO object is created by reflection

    2). SpringMVC assigns the request parameters of the form to the properties corresponding to the target of WebDataBinder.

    3). SpringMVC will give the attrName and target of WebDataBinder to implicitModel, and then pass it to the request domain object.

    4). Pass the target of WebDataBinder as a parameter to the input parameter of the target method.

Running process:

  1. Execute the method modified by the @ModelAttribute annotation: Take the object from the database and put the object into the Map. The key is: user

  2. SpringMVC takes the User object from the Map, and assigns the request parameters of the form to the corresponding properties of the User object.

  3. SpringMVC passes the above objects into the parameters of the target method.
    Note: In the method modified by @ModelAttribute, the key put into the Map needs to be the same as the lowercase string of the first letter of the input parameter type of the target method!

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

  1. Determine a key:

    • If the parameter of the POJO type of the target method is not decorated with @ModelAttribute, the key is the lowercase of the first letter of the POJO class name
    • If it is decorated with @ModelAttribute, the key is the value of the value attribute of the @ModelAttribute annotation.
  2. Find the object corresponding to the key in implicitModel, and if it exists, pass it as an input parameter

    • If it has been saved in the Map in the method marked with @ModelAttribute, and the key is the same as the key determined by 1, it will be obtained.
    • If the object corresponding to key does not exist in implicitModel, check whether the current Handler is decorated with @SessionAttributes annotation,
    • If this annotation is used, and the value of the @SessionAttributes annotation contains the key, the value corresponding to the key will be obtained from the HttpSession, and if it exists, it will be directly passed to the input parameter of the target method. If it does not exist An exception will be thrown.
    • If the Handler does not identify the @SessionAttributes annotation or the value of the @SessionAttributes annotation does not contain a key, a POJO type parameter will be created through reflection and passed in as the parameter of the target method
    • SpringMVC will save objects of type key and POJO to implicitModel, which in turn will be saved to request.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690513&siteId=291194637