ModelMap的作用

ModelMap的作用最近在用ssm框架开发项目,之前都是二次开发,现在是从头开发,都有点不适应了,遇到了很多前端后台的问题。说说ModelMap的作用packageorg.springframework.ui;importjava.util.Collection;importjava.util.LinkedHashMap;importjava.util.Map;importorg.spr

大家好,又见面了,我是你们的朋友全栈君。

最近在用ssm框架开发项目,之前都是二次开发,现在是从头开发,都有点不适应了,遇到了很多前端后台的问题。

说说ModelMap的作用

package org.springframework.ui;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.core.Conventions;
import org.springframework.util.Assert;

@SuppressWarnings("serial")
public class ModelMap extends LinkedHashMap<String, Object> {

	
	public ModelMap() {
	}

	
	public ModelMap(String attributeName, Object attributeValue) {
		addAttribute(attributeName, attributeValue);
	}

	
	public ModelMap(Object attributeValue) {
		addAttribute(attributeValue);
	}


	
	public ModelMap addAttribute(String attributeName, Object attributeValue) {
		Assert.notNull(attributeName, "Model attribute name must not be null");
		put(attributeName, attributeValue);
		return this;
	}

	
	public ModelMap addAttribute(Object attributeValue) {
		Assert.notNull(attributeValue, "Model object must not be null");
		if (attributeValue instanceof Collection && ((Collection<?>) attributeValue).isEmpty()) {
			return this;
		}
		return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
	}

	
	public ModelMap addAllAttributes(Collection<?> attributeValues) {
		if (attributeValues != null) {
			for (Object attributeValue : attributeValues) {
				addAttribute(attributeValue);
			}
		}
		return this;
	}

	
	public ModelMap addAllAttributes(Map<String, ?> attributes) {
		if (attributes != null) {
			putAll(attributes);
		}
		return this;
	}

	
	public ModelMap mergeAttributes(Map<String, ?> attributes) {
		if (attributes != null) {
			for (Map.Entry<String, ?> entry : attributes.entrySet()) {
				String key = entry.getKey();
				if (!containsKey(key)) {
					put(key, entry.getValue());
				}
			}
		}
		return this;
	}

	
	public boolean containsAttribute(String attributeName) {
		return containsKey(attributeName);
	}

}

代码里面有个方法:addAttribute和addAttributeAll

这个方法其实就是我们经常用的,在代码里面一般是这样体现的:

map.put("smallGoods", smallGoods);

就是把后台查询出来的集合、字符串(一般是错误信息)、对象等传到前台,当然,这个值不能是空的

前台接收参数时:两个问号是判断这个对象或者集合是否为空

<#if smallGoods??>
 <#list smallGoods as specialControl>
		<option id="${specialControl.goodstypeid}" name="tempgoodstypeid" value="${specialControl.goodstypeid}">${specialControl.goodsdesc}</option>
	</#list>
<#else>
</#if> 


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150106.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月18日 下午8:16
下一篇 2022年6月18日 下午8:16


相关推荐

  • 大数据:数据采集平台之Apache Chukwa「建议收藏」

    大数据:数据采集平台之Apache Chukwa「建议收藏」大数据:数据采集平台之ApacheChukwa官网:https://chukwa.apache.org/ApacheChukwa是Apache旗下除ApacheFlume外,又一个开源的数据收集平台。Chukwa基于Hadoop的HDFS和MapReduce来构建(显而易见,它用Java来实现),提供扩展性和可靠性。Chukwa同时提供对数据的展示,分析和监视。该项目已经不活跃了,它…

    2022年5月29日
    33
  • static–静态变量与普通变量的区别

    static–静态变量与普通变量的区别静态变量与普通变量的区别全局变量(外部变量)的说明之前再冠以static就构成了静态的全局变量。全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式。这两者在存储方式上并无不同。这两者的区别虽在于非静态全局变量的作用域是整个源程序,当一个源程序由多个源文件组成时,非静态的全局变量在各个源文件中都是有效的。而静态全局变量则限制了其作用域,即只在定义该变量的源文件内有效,…

    2022年5月3日
    52
  • PCI设备驱动解析

    PCI设备驱动解析首先我想引用一下官方的源代码中的文档 对设备驱动的工作做一个概括性的叙述 1 首先是调用 pci register driver 函数 对设备进行注册 2 Oncethedrive thedrivergen Enablethedev RequestMMIO IOPresourc

    2026年3月16日
    2
  • 经验干货:使用tf.py_func函数增加Tensorflow程序的灵活性

    经验干货:使用tf.py_func函数增加Tensorflow程序的灵活性nbsp nbsp 不知不觉 笔者接触 Tensorflow 也满一年了 在这一年当中 笔者对 Tensorflow 的了解程度也逐渐加深 相比笔者接触的第一个深度学习框架 Caffe 而言 笔者认为 Tensorflow 更适合科研一些 网络搭建与算法设置的自由度也更大 使用 Tensorflow 实现自己的算法也更迅速 nbsp nbsp 但是 笔者认为 Tensorflow 还是有不足的地方 第一体现在 Tensorflow 的数据机制 由于 te

    2026年3月17日
    3
  • 罗技键盘k380打不了字_罗技k380键盘配对成功后无法使用

    罗技键盘k380打不了字_罗技k380键盘配对成功后无法使用mac连接成功罗技k380键盘,但是发现数字键上面的字符对不上,很多字符以及标点符号都打不出来,是什么原因导致的呢?问题分析首先,分析一下,到底是什么原因导致的?可以连接其他设备试一试,比如我发现k380键盘在我的ipad上是可以正常使用的。那么就排除了键盘本身存在问题,坏了等猜测。如果不是键盘本身出了问题,那么我们就要去分析问题究竟出现在哪里?联系了客服,加上自己对键盘配对过程的回顾,大致判定问题出现在最开始的匹配的时候,选错了【键盘类型】。解决问题-重设【键盘类型】步骤如下:【系统偏好设置

    2022年10月9日
    3
  • savefiledialog用法_空白文档的默认文件名

    savefiledialog用法_空白文档的默认文件名把xml文档转换为excel文档,希望保存时默认的文件名是xml文档的同名.xlsx。

    2022年10月8日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号