sss following the article "SpringBoot: principle of parameter processing -- 8.2", we continue to analyze the request principle of complex parameters. In fact, the process is similar to that of the previous article, that is, how to deal with the mv return in a disguised form.
Complex parameters
sssMap and model (the data in map and model will be placed in the request field of request, request.setAttribute)
sssErrors/BindingResult, RedirectAttributes (data carried by redirection), ServletResponse (response)
sssSessionStatus,UriComponentsBuilder,ServletUriComponentsBuilder
Map<String,Object> map, Model model, HttpServletRequest request All can be given request Put data in the domain, request.getAttribute(); Fetch data
example:
@GetMapping("/params") public String testParam(Map<String,Object> map , Model model, HttpServletRequest requst, HttpServletResponse response){ map.put("hello" , "world666"); model.addAttribute("world","Hello666"); requst.setAttribute("message" , "HelloWorld"); Cookie cookie = new Cookie("c1" , "v1"); cookie.setDomain("localhost"); response.addCookie(cookie); return "forward:/success"; } @ResponseBody @GetMapping("/success") public Map success(HttpServletRequest request){ Object msg1 = request.getAttribute("msg"); HashMap<String, Object> map = new HashMap<>(); Object hello = request.getAttribute("hello"); Object world = request.getAttribute("world"); Object message = request.getAttribute("message"); map.put("reqMethod_msg",msg1); map.put("annotation_msg",msg); map.put("hello",hello); map.put("world",world); map.put("message",message); return map; } }
sss results:
{"world":"Hello666","hello":"world666","message":"HelloWorld"}
dasdads
sdsss [Conclusion]: the response forwarding Cookie is also OK. Map < string, Object > map, model model, HttpServletRequest request request can put data into the request field
Why are map and model parameters stored in the same location after parsing? (yes)
Through deBug debugging of sdsss, we found that:
Through the source code of sss, we can know that the parameters of Map and Model types are parsed and finally the same address is returned.
After sdsss parses the parameters, it needs to execute the method body. However, we found that the map and model related parameters stored in the parsed parameters are placed in the mavContainer with the same address, and the return value is forward:/success.
How are the parameters and forwarding addresses parsed by Model and View encapsulated in mav and returned?
sdsss needs to process the return value after executing the method body:
How does sdsss handle return values? Through the above two figures, we can find that all data is placed in the ModelAndViewContainer; Contains the page address View (return value) to go to. It also contains Model (map) data.
After sdsss processing, we need to get ModelAndView, and then return:
sdsdsdsdsssvar15 = this.getModelAndView(mavContainer, modelFactory, webRequest);
sdsss ① the first thing to do in this method is to see whether to update the model (that is, do some binding work):
After sdsss ②, bind or unbind, re obtain the Model, find that the address has changed, and then re encapsulate it into ModelAndView. Then judge whether it is redirected data. If so, process it again.
sdsss ③ finally, return ModelAndView and the corresponding doDispatcher()
sdsdsdssmv = ha.handle(processedRequest, response, mappedHandler.getHandler()); This method is over.
What about the processed data (that is, the data in mav)?
The corresponding method of sdsss is firstly mappedHandler.applyPostHandle(processedRequest, response, mv) in doDispatcher();, This is the work related to the interceptor.
After the execution of sdsss, you will jump to the most important method: processing the distribution results!!! He will certainly put things in the model into the request domain:
sddsdssssthis.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);
sdsss ① first, handle some errors:
sdsss
sdsss ② analyze render() method:
When sdsss gets the URL address of the corresponding view, it starts forwarding:
view.render(mv.getModelInternal(), request, response); =====>The core methods are as follows Map<String, Object> mergedModel = this.createMergedOutputModel(model, request, response); ====>Click to enter: if (model != null) { mergedModel.putAll(model);} =====>That is to say model All data in is returned to mergedModel Medium, and mergedModel yes linkedHashmap.💦
sdsss is going to forward the merged model:
sdsss ① and the source code of exposedmodelasrequestattributes. We can find that by traversing, we can put the objects in the model into the request, and then forward them.