1 16 17 package org.springframework.ui; 18 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.springframework.core.Conventions; 25 import org.springframework.util.Assert; 26 27 40 public class ModelMap extends HashMap { 41 42 45 public ModelMap() { 46 } 47 48 53 public ModelMap(String modelName, Object modelObject) { 54 addObject(modelName, modelObject); 55 } 56 57 63 public ModelMap(Object modelObject) { 64 addObject(modelObject); 65 } 66 67 68 73 public ModelMap addObject(String modelName, Object modelObject) { 74 Assert.notNull(modelName, "Model name must not be null"); 75 this.put(modelName, modelObject); 76 return this; 77 } 78 79 88 public ModelMap addObject(Object modelObject) { 89 Assert.notNull(modelObject, "Model object must not be null"); 90 if (modelObject instanceof Collection && ((Collection ) modelObject).isEmpty()) { 91 return this; 92 } 93 return addObject(Conventions.getVariableName(modelObject), modelObject); 94 } 95 96 99 public ModelMap addAllObjects(Map objects) { 100 if (objects != null) { 101 this.putAll(objects); 102 } 103 return this; 104 } 105 106 111 public ModelMap addAllObjects(Collection objects) { 112 if (objects != null) { 113 for (Iterator it = objects.iterator(); it.hasNext();) { 114 addObject(it.next()); 115 } 116 } 117 return this; 118 } 119 120 } 121 | Popular Tags |