Spring MVCModel、Map及ModelMap几种方式

Spring MVC输出数据的几种方式

@RequestMapping("/handle01")
    public ModelAndView handle01(){
        LocalDateTime now = LocalDateTime.now();
        //封装页面与数据
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date",now);
        //封装跳转的页面
        modelAndView.setViewName("success");
        System.err.println("==========ModelAndView======== "+modelAndView.getClass());
        return  modelAndView;
    }

    @RequestMapping("/handle11")
    public String handle11(ModelMap modelMap){
        LocalDateTime now = LocalDateTime.now();
        modelMap.addAttribute("date",now);
        System.err.println("==========modelMap======== "+modelMap.getClass());
        return  "success";
    }

    @RequestMapping("/handle12")
    public String handle12(Model model){
        LocalDateTime now = LocalDateTime.now();
        model.addAttribute("date",now);
        System.err.println("==========model======== "+model.getClass());
        return  "success";
    }
    @RequestMapping("/handle13")
    public String handle12(Map<String,Object> map){
        LocalDateTime now = LocalDateTime.now();
        map.put("date",now);
        System.err.println("==========map======== "+map.getClass());
        return  "success";
    }
  • ModelAndView
  • ModelMap
  • Model
  • Map
    都可以成功跳转到指定页面

==========ModelAndView======== class org.springframework.web.servlet.ModelAndView
==========modelMap======== class org.springframework.validation.support.BindingAwareModelMap
==========model======== class org.springframework.validation.support.BindingAwareModelMap
==========map======== class org.springframework.validation.support.BindingAwareModelMap

在这里插入图片描述
后面3个都是BindingAwareModelMap,数据都会被放在这个中

类结构图
在这里插入图片描述

BindingAwareModelMap是最底层的最后都要转换为BindingAwareModelMap
ModelMap Model Map最后都到转换为BindingAwareModelMap

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值