zoukankan      html  css  js  c++  java
  • 数据输出机制之Model、Map及ModelMap回顾

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

    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

  • 相关阅读:
    poj 3040 Allowance
    poj 2393 Yogurt factory
    【BZOJ1833】数字计数(ZJOI2010)-数位DP
    【BZOJ4820】硬币游戏(SDOI2017)-概率+高斯消元+KMP
    【BZOJ3626】LCA(LNOI2014)-树链剖分+离线处理
    【BZOJ4817】树点涂色(SDOI2017)-LCT+LCA+线段树
    【BZOJ1135】LYZ(POI2009)-线段树+Hall定理
    【CF392D】Three Arrays-set+multiset
    【51Nod1688】LYKMUL-线段树+乘法原理
    【BZOJ2956】模积和-数论分块
  • 原文地址:https://www.cnblogs.com/liuyj-top/p/13137761.html
Copyright © 2011-2022 走看看