数据输出机制之Model、Map及ModelMap回顾

本文回顾了在Spring框架中,数据输出常用的Model、Map以及ModelMap机制。通过实例展示了它们如何实现数据传递,并强调了最终执行的是BindingAwareModelMap。同时,提到了ModelMap基于LinkedHashMap,而Model和Map均属于java.util包。
摘要由CSDN通过智能技术生成

我们用不同的方式来实现数据的 传递:

package com.lagou.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import java.util.Date;
import java.util.Map;


/**
* @author lyj
* @Title: DemoController
* @ProjectName springmvc-demo
* @Description: TODO
* @date 2020/6/9 21:21
*/
@Controller
@RequestMapping("/demo")
public class DemoController {
    /**
     * http://localhost:8080/demo/handle01
     */
    @RequestMapping("/handle01")
    public ModelAndView handle01(){
        Date date=new Date();


        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("date",date);
        modelAndView.setViewName("success");
        System.out.println("modelAndView----"+modelAndView.getClass());


        return modelAndView;
    }


    /**
     * http://localhost:8080/demo/handle02
     */
    @RequestMapping("/handle02")
    public String handle02(ModelMap modelMap){
        Date date=new Date();


        modelMap.addAttribute("date",date);
        System.out.println("modelMap------"+modelMap.getClass());


        return "success";
    }


    /**
     * http://localhost:8080/demo/handle03
     */
    @RequestMapping("/handle03")
    public String handle03(Model model){
        Date date=new Date();
        model.addAttribute("date",date);
        System.out.println("model-----"+model.getClass());


        return "success";
    }
    /**
     * http://localhost:8080/demo/handle04
     */
    @RequestMapping("/handle04")
    public String handle04(Map<String,Object>map){
        Date date=new Date();
       map.put("date",date);
        System.out.println("map------"+map.getClass());


        return "success";
    }
}

下面我们来看输出结果:

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

说明他们最后都是执行的BindingAwareModelMap
ModelMap:

LinkedHashMap是jdk里面的 package java.util;

Model:

Map:

Map也是java.util下面的

BindingAwareModelMap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员资料站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值