SpringBoot核心技术-Web开发-复杂参数

MapModel(map、model里面的数据会被放在request的请求域 request.setAttribute)、Errors/BindingResult、RedirectAttributes( 重定向携带数据)ServletResponse(response)、SessionStatus、UriComponentsBuilder、ServletUriComponentsBuilder

1、测试

@Controller
public class RequestController {
    @GetMapping("/params")
    public String textParam(Map<String, Object> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response) {
        map.put("map-msg", "map-hello");
        model.addAttribute("model-msg", "model-hello");
        request.setAttribute("request-msg","request-hello");
        response.addCookie(new Cookie("c1","v1"));
        return "forward:/success";
    }

    @GetMapping("/success")
    @ResponseBody
    public Map success(@RequestAttribute(value = "msg",required = false) String msg,HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        map.put("map-msg", request.getAttribute("map-msg"));
        map.put("model-msg", request.getAttribute("model-msg"));
        map.put("request-msg", request.getAttribute("request-msg"));
        return map;
    }

}

运行结果: 

 

2、 原理

具体流程和普通参数相同,请求参数解析过程

Map的参数解析器:MapMethodProcessor

Modell的参数解析器:ModelMethodProcessor

这两个参数解析器resolvers.resolveArgument内部解析代码都会返回 mavContainer.getModel(),即Model类型参数和Map类型参数最终指向同一个对象。

mavContainer是ModelAndViewContainer类型,ModelAndViewContainer的getModel()返回的是BindingAwareModelMap对象,是Model也是Map。

因此在控制器的方法中,对Model类型请求参数和Map类型请求参数赋值,最终都会添加到同一个ModelAndViewContainer中的BindingAwareModelMap中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值