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

  

posted @ 2020-01-03 12:03  GordonDicaprio  阅读(1004)  评论(0编辑  收藏  举报