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是允許的