1 16 17 package org.springframework.web.servlet; 18 19 import java.util.Map ; 20 21 import org.springframework.ui.ModelMap; 22 23 43 public class ModelAndView { 44 45 46 private Object view; 47 48 49 private ModelMap model; 50 51 54 private boolean cleared; 55 56 57 63 public ModelAndView() { 64 } 65 66 73 public ModelAndView(String viewName) { 74 this.view = viewName; 75 } 76 77 83 public ModelAndView(View view) { 84 this.view = view; 85 } 86 87 95 public ModelAndView(String viewName, Map model) { 96 this.view = viewName; 97 if (model != null) { 98 getModelMap().addAllObjects(model); 99 } 100 } 101 102 112 public ModelAndView(View view, Map model) { 113 this.view = view; 114 if (model != null) { 115 getModelMap().addAllObjects(model); 116 } 117 } 118 119 126 public ModelAndView(String viewName, String modelName, Object modelObject) { 127 this.view = viewName; 128 addObject(modelName, modelObject); 129 } 130 131 137 public ModelAndView(View view, String modelName, Object modelObject) { 138 this.view = view; 139 addObject(modelName, modelObject); 140 } 141 142 143 148 public void setViewName(String viewName) { 149 this.view = viewName; 150 } 151 152 156 public String getViewName() { 157 return (this.view instanceof String ? (String ) this.view : null); 158 } 159 160 164 public void setView(View view) { 165 this.view = view; 166 } 167 168 172 public View getView() { 173 return (this.view instanceof View ? (View) this.view : null); 174 } 175 176 180 public boolean hasView() { 181 return (this.view != null); 182 } 183 184 189 public boolean isReference() { 190 return (this.view instanceof String ); 191 } 192 193 197 protected Map getModelInternal() { 198 return this.model; 199 } 200 201 204 public ModelMap getModelMap() { 205 if (this.model == null) { 206 this.model = new ModelMap(); 207 } 208 return this.model; 209 } 210 211 215 public Map getModel() { 216 return getModelMap(); 217 } 218 219 220 225 public ModelAndView addObject(Object modelObject) { 226 getModelMap().addObject(modelObject); 227 return this; 228 } 229 230 237 public ModelAndView addObject(String modelName, Object modelObject) { 238 getModelMap().addObject(modelName, modelObject); 239 return this; 240 } 241 242 248 public ModelAndView addAllObjects(Map modelMap) { 249 getModelMap().addAllObjects(modelMap); 250 return this; 251 } 252 253 254 262 public void clear() { 263 this.view = null; 264 this.model = null; 265 this.cleared = true; 266 } 267 268 272 public boolean isEmpty() { 273 return (this.view == null && this.model == null); 274 } 275 276 283 public boolean wasCleared() { 284 return (this.cleared && isEmpty()); 285 } 286 287 288 291 public String toString() { 292 StringBuffer buf = new StringBuffer ("ModelAndView: "); 293 if (isReference()) { 294 buf.append("reference to view with name '").append(this.view).append("'"); 295 } 296 else { 297 buf.append("materialized View is [").append(this.view).append(']'); 298 } 299 buf.append("; model is ").append(this.model); 300 return buf.toString(); 301 } 302 } 303 | Popular Tags |