Spring modelMap.put()v/s modelMap.addAttribute()
在春天,两者的区别是什么Spring modelMap.put()v/s modelMap.addAttribute(),spring,spring-mvc,jakarta-ee,Spring,Spring Mvc,Jakarta Ee,在春天,两者的区别是什么 modelMap.put("key",value); 及 addAttributes意味着在属性名称->查看源中检查是否不为空 /** * Add the supplied attribute under the supplied name. * @param attributeName the name of the model attribute (never <code>null</code>) * @pa
modelMap.put("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;
}
在提供的名称下添加提供的属性
将指定的值与中指定的attributeName相关联
这张地图。如果映射以前包含
attributeName,则替换旧值
addAttribute用于添加值,put用于添加或替换
如果考虑java Spring API < /P> java.lang.Object java.util.AbstractMap java.util.HashMap java.util.LinkedHashMap org.springframework.ui.ModelMap
Spring框架是从HashMap继承而来的,put是一种方法 继承的表单HashMap这是不正确的,如果您检查实现,您将看到这两个方法是相同的,除了addattribute检查null。
/**
* 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)