1 16 17 package org.springframework.web.portlet; 18 19 import java.util.Map ; 20 21 import org.springframework.ui.ModelMap; 22 23 42 public class ModelAndView { 43 44 45 private Object view; 46 47 48 private ModelMap model; 49 50 53 private boolean cleared; 54 55 56 62 public ModelAndView() { 63 } 64 65 72 public ModelAndView(String viewName) { 73 this.view = viewName; 74 } 75 76 82 public ModelAndView(Object view) { 83 this.view = view; 84 } 85 86 94 public ModelAndView(String viewName, Map model) { 95 this.view = viewName; 96 if (model != null) { 97 getModelMap().addAllObjects(model); 98 } 99 } 100 101 108 public ModelAndView(Object view, Map model) { 109 this.view = view; 110 if (model != null) { 111 getModelMap().addAllObjects(model); 112 } 113 } 114 115 122 public ModelAndView(String viewName, String modelName, Object modelObject) { 123 this.view = viewName; 124 addObject(modelName, modelObject); 125 } 126 127 133 public ModelAndView(Object view, String modelName, Object modelObject) { 134 this.view = view; 135 addObject(modelName, modelObject); 136 } 137 138 139 144 public void setViewName(String viewName) { 145 this.view = viewName; 146 } 147 148 152 public String getViewName() { 153 return (this.view instanceof String ? (String ) this.view : null); 154 } 155 156 163 public void setView(Object view) { 164 this.view = view; 165 } 166 167 171 public Object getView() { 172 return (!(this.view instanceof String ) ? this.view : null); 173 } 174 175 179 public boolean hasView() { 180 return (this.view != null); 181 } 182 183 188 public boolean isReference() { 189 return (this.view instanceof String ); 190 } 191 192 196 protected Map getModelInternal() { 197 return this.model; 198 } 199 200 203 public ModelMap getModelMap() { 204 if (this.model == null) { 205 this.model = new ModelMap(); 206 } 207 return this.model; 208 } 209 210 214 public Map getModel() { 215 return getModelMap(); 216 } 217 218 219 224 public ModelAndView addObject(Object modelObject) { 225 getModelMap().addObject(modelObject); 226 return this; 227 } 228 229 236 public ModelAndView addObject(String modelName, Object modelObject) { 237 getModelMap().addObject(modelName, modelObject); 238 return this; 239 } 240 241 247 public ModelAndView addAllObjects(Map modelMap) { 248 getModelMap().addAllObjects(modelMap); 249 return this; 250 } 251 252 253 261 public void clear() { 262 this.view = null; 263 this.model = null; 264 this.cleared = true; 265 } 266 267 271 public boolean isEmpty() { 272 return (this.view == null && this.model == null); 273 } 274 275 282 public boolean wasCleared() { 283 return (this.cleared && isEmpty()); 284 } 285 286 287 290 public String toString() { 291 StringBuffer buf = new StringBuffer ("ModelAndView: "); 292 if (isReference()) { 293 buf.append("reference to view with name '").append(this.view).append("'"); 294 } 295 else { 296 buf.append("materialized View is [").append(this.view).append(']'); 297 } 298 buf.append("; model is ").append(this.model); 299 return buf.toString(); 300 } 301 302 } 303 | Popular Tags |