在春天,是什麼modelMap.put()V/S modelMap.addAttribute()
modelMap.put("key",value);
和
modelMap.addAttribute("Key",value);
在春天,是什麼modelMap.put()V/S modelMap.addAttribute()
modelMap.put("key",value);
和
modelMap.addAttribute("Key",value);
addAttributes之間的差異意味着屬性名稱檢查不爲空 - >見來源
/**
* Add the supplied attribute under the supplied name.
* @param attributeName the name of the model attribute (never <code>null</code>)
* @param attributeValue the model attribute value (can be <code>null</code>)
*/
public ModelMap addAttribute(String attributeName, Object attributeValue) {
Assert.notNull(attributeName, "Model attribute name must not be null");
put(attributeName, attributeValue);
return this;
}
addAttribute(String attributeName, Object attributeValue)
在提供的名稱下添加提供的屬性。
put(String attributeName, Object attributeValue)
將指定值與 此映射指定的attributeName。如果映射先前包含 attributeName的映射,則舊值將被替換。
的addAttribute是增加價值,並把用於添加或更換
如果我們考慮到有關Java API春
java.lang.Object繼承 java.util.AbstractMap中 的java.util.HashMap 的java .util.LinkedHashMap org.springframework.ui.ModelMap
Spring Framework已經從HashMap繼承,並放置了一個方法 繼承形式HashMap。
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/ui/ModelMap.html
這是不正確的,如果你檢查,你會看到,這兩種方法都只是的addAttribute檢查空相同的實行。 – Aladdin