Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring modelMap.put()v/s modelMap.addAttribute()_Spring_Spring Mvc_Jakarta Ee - Fatal编程技术网

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)