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

      

  • 相关阅读:
    centos7 源码安装python3
    gitlab 迁移
    gitlab 搭建
    亚伦史沃茨 公开信
    误删了sudo包怎么办
    在中国历史上,我个人最喜欢的三个皇帝
    安装PHP7
    Mac OS上设置Django开发环境
    The Pragmatic Programmer Quick Reference Guide
    Nginx的第一个模块-HelloWorld
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/12144258.html
Copyright © 2011-2022 走看看