ModelMap对象的 addAttribute,put两个方法有什么区别?

如题所述

addAttribute,put两个方法继承的方法不同。

具体见解如下:

    两者的继承路径不同。ModelMap是继承自LinkedHashMap的,而LinkedHashMap继承自HashMap,HashMap实现了Map接口,实现其put方法,因此ModelMap中的put方法是继承自 HashMap的;

    addAttribute可以检查key值是否为空。其实也是调用的put方法,但是会在调用之前判断 key值是否为null,如果为null则会报错

    java.lang.IllegalArgumentException: Model attribute name must not be null,而put方法不会检查key值是否会空。

    总而言之,ModelMap对象的 addAttribute,put两个方法有什么区别就是  addAttribute是不允许添加空值的key,put是允许的。

温馨提示:答案为网友推荐,仅供参考
相似回答