ModelMap中的addAttribute与put方法的区别

    xiaoxiao2026-03-01  9

    ModelMap的定义类: public class ModelMap extends LinkedHashMap<String, Object> ModelMap是继承自LinkedHashMap的,而LinkedHashMap继承自HashMap,HashMap实现了Map接口,实现其put()方法,因此ModelMap中的put()方法是继承自 HashMap的; addAttribute方法的定义如下: public ModelMap addAttribute(String attributeName, Object attributeValue){ Assert.notNull(attributeName, "Model attribute name must not be null"); put(attributeName, attributeValue); return this; }其实也是调用的put方法,但是会在调用之前判断 key值是否为null,如果为null则会抛出以下异常 java.lang.IllegalArgumentException: Model attribute name must not be null,而put方法不会检查key值是否会空 结论: ModelMap对象的 addAttribute,put两个方法区别是: addAttribute是不允许添加空值的key,put是允许的
    转载请注明原文地址: https://ju.6miu.com/read-1307517.html
    最新回复(0)