zoukankan      html  css  js  c++  java
  • SpringMVC中重定向参数的使用以及原理解析 RedirectAttributes

    SpringMVC中重定向参数的使用以及原理解析
    
    OUTPUT_FLASH_MAP_ATTRIBUTE::org.springframework.web.servlet.DispatcherServlet#OUTPUT_FLASH_MAP_ATTRIBUTE
    INPUT_FLASH_MAP_ATTRIBUTE::org.springframework.web.servlet.DispatcherServlet#INPUT_FLASH_MAP_ATTRIBUTE
    
    
    
    springMVC通过org.springframework.web.servlet.support.RequestContextUtils#getOutputFlashMap方法获取OUTPUT_FLASH_MAP_ATTRIBUTE标识下的map,并向其中放入数据。
    参考(spring-webmvc-5.2.0.RELEASE.jar)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#getModelAndView方法下的(1010行)
    
    
    然后通过org.springframework.web.servlet.support.RequestContextUtils#saveOutputFlashMap方法进行设置
    步骤如下:首先调用org.springframework.web.servlet.support.RequestContextUtils#getOutputFlashMap方法拿到之前放入的数据,
    然后通过(281行)manager.saveOutputFlashMap(flashMap, request, response);
    放入缓存中。
    
    
    OUTPUT_FLASH_MAP_ATTRIBUTE和INPUT_FLASH_MAP_ATTRIBUTE仅仅是检索和保存的标识,
    实际数据存储在session数据的该org.springframework.web.servlet.support.SessionFlashMapManager#FLASH_MAPS_SESSION_ATTRIBUTE标识下
    参考类:org.springframework.web.servlet.support.SessionFlashMapManager
    
    
    使用方法:
    
    set数据
    使用如下方法存入要在重定向之后使用的数据:
    使用RedirectAttributes参数类型
    	@RequestMapping(value="xxxx", method=RequestMethod.POST)
    	public String addCustomer(final RedirectAttributes redirectAttributes) {
    		redirectAttributes.addFlashAttribute("customer", customer);
    		redirectAttributes.addFlashAttribute("message","Added successfully.");
    		return "redirect:xxxxxx.html";	
    	}
    实现原理在:org.springframework.web.servlet.mvc.method.annotation.RedirectAttributesMethodArgumentResolver 使用参数解析处理方法
    
    
    
    get数据
    重定向之后,会将上述数据直接放入Model、Map、ModelAndView等数据模型中,可直接使用。
    源码位置:(spring-webmvc-5.2.0.RELEASE.jar)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#invokeHandlerMethod下的
    (864行)mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
    

      

  • 相关阅读:
    【洛谷P5305】旧词
    【洛谷P5470】序列
    【CF865D】Buy Low Sell High
    【洛谷P3242】接水果
    【洛谷P5048】Yuno loves sqrt technology III
    一、Java语言基础(1)_走进java——JDK-JRE-JVM概述
    一、Java语言基础(1)_走进java——跨平台/可移植性
    第一阶段:前端开发_JavaScript基础
    第一阶段:前端开发_HTML表单&CSS
    第一阶段:前端开发_HTML&CSS
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/12144258.html
Copyright © 2011-2022 走看看