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));