ModelAndView与ModelMap的源码

1、ModelAndView类  copy

  1. public class ModelAndView {

  2.  private Object view;//该属性用来存储返回的视图信息
  3.  private ModelMap model;//该属性用来存储处理后的结果数据 
  4.  private boolean cleared = false;  

  5.  public ModelAndView() {  
  6.  }  

  7.  public ModelAndView(String viewName) {  
  8.     this.view = viewName;  
  9.  }    
  10.   
  11.  public ModelAndView(View view) {  
  12.     this.view = view;  
  13.  }    
  14.  
  15.  public ModelAndView(String viewName, Map<String, ?> model) {  
  16.     this.view = viewName;  
  17.     if (model != null) {  
  18.         getModelMap().addAllAttributes(model);  
  19.     }  
  20.  }  
  21.  
  22.  public ModelAndView(View view, Map<String, ?> model) {  
  23.     this.view = view;  
  24.     if (model != null) {  
  25.         getModelMap().addAllAttributes(model);  
  26.     }  
  27.  }   

  28.  public ModelAndView(String viewName, String modelName, Object modelObject) {  
  29.     this.view = viewName;  
  30.     addObject(modelName, modelObject);  
  31.  }    

  32.  public ModelAndView(View view, String modelName, Object modelObject) {  
  33.     this.view = view;  
  34.     addObject(modelName, modelObject);  
  35.  }    
  36.  
  37.  public void setViewName(String viewName) {  
  38.     this.view = viewName;  
  39.  }    

  40.  public String getViewName() {  
  41.     return (this.view instanceof String ? (String) this.view : null);  
  42.  }    
  43.  
  44.  public void setView(View view) {  
  45.     this.view = view;  
  46.  }    
  47.  
  48.  public View getView() {  
  49.     return (this.view instanceof View ? (View) this.view : null);  
  50.  }  
  51.  
  52.  public boolean hasView() {  
  53.     return (this.view != null);  
  54.  }  

  55.  public boolean isReference() {  
  56.     return (this.view instanceof String);  
  57.  }  
  58.  
  59.  protected Map<String, Object> getModelInternal() {  
  60.     return this.model;  
  61.  }  
  62.  
  63.  public ModelMap getModelMap() {  
  64.     if (this.model == null) {  
  65.         this.model = new ModelMap();  
  66.     }  
  67.     return this.model;  
  68.  }  

  69.  public Map<String, Object> getModel() {  
  70.     return getModelMap();  
  71.  }  

  72.  public ModelAndView addObject(String attributeName, Object attributeValue) {  
  73.     getModelMap().addAttribute(attributeName, attributeValue);  
  74.     return this;  
  75.  }  

  76.  public ModelAndView addObject(Object attributeValue) {  
  77.     getModelMap().addAttribute(attributeValue);  
  78.     return this;  
  79.  }  

  80.  public ModelAndView addAllObjects(Map<String, ?> modelMap) {  
  81.     getModelMap().addAllAttributes(modelMap);  
  82.     return this;  
  83.  }  

  84.  public void clear() {  
  85.     this.view = null;  
  86.     this.model = null;  
  87.     this.cleared = true;  
  88.  }  

  89.  public boolean isEmpty() {  
  90.     return (this.view == null && CollectionUtils.isEmpty(this.model));  
  91.  }  

  92.  public boolean wasCleared() {  
  93.     return (this.cleared && isEmpty());  
  94.  }  

  95.  @Override  
  96.  public String toString() {  
  97.     StringBuilder sb = new StringBuilder("ModelAndView: ");  
  98.     if (isReference()) {  
  99.         sb.append("reference to view with name '").append(this.view).append("'");  
  100.     }  
  101.     else {  
  102.         sb.append("materialized View is [").append(this.view).append(']');  
  103.     }  
  104.     sb.append("; model is ").append(this.model);  
  105.     return sb.toString();  
  106.  }  

  107. }

2、ModelMap类

  1. public class ModelMap extends LinkedHashMap<String, Object> {  
  2.         
  3.     public ModelMap() {  
  4.     }  
  5.   
  6.     public ModelMap(String attributeName, Object attributeValue) {  
  7.         addAttribute(attributeName, attributeValue);  
  8.     }  
  9.   
  10.     public ModelMap(Object attributeValue) {  
  11.         addAttribute(attributeValue);  
  12.     }    
  13.   
  14.     public ModelMap addAttribute(String attributeName, Object attributeValue) {  
  15.         Assert.notNull(attributeName, "Model attribute name must not be null");  
  16.         put(attributeName, attributeValue);  
  17.         return this;  
  18.     }  
  19.   
  20.     public ModelMap addAttribute(Object attributeValue) {  
  21.         Assert.notNull(attributeValue, "Model object must not be null");  
  22.         if (attributeValue instanceof Collection && ((Collection<?>) attributeValue).isEmpty()) {  
  23.             return this;  
  24.         }  
  25.         return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);  
  26.     }  
  27.   
  28.     public ModelMap addAllAttributes(Collection<?> attributeValues) {  
  29.         if (attributeValues != null) {  
  30.             for (Object attributeValue : attributeValues) {  
  31.                 addAttribute(attributeValue);  
  32.             }  
  33.         }  
  34.         return this;  
  35.     }    
  36.   
  37.     public ModelMap addAllAttributes(Map<String, ?> attributes) {  
  38.         if (attributes != null) {  
  39.             putAll(attributes);  
  40.         }  
  41.         return this;  
  42.     }    
  43.   
  44.     public ModelMap mergeAttributes(Map<String, ?> attributes) {  
  45.         if (attributes != null) {  
  46.             for (Map.Entry<String, ?> entry : attributes.entrySet()) {  
  47.                 String key = entry.getKey();  
  48.                 if (!containsKey(key)) {  
  49.                     put(key, entry.getValue());  
  50.                 }  
  51.             }  
  52.         }  
  53.         return this;  
  54.     }    
  55.       
  56.    public boolean containsAttribute(String attributeName) {  
  57.         return containsKey(attributeName);  
  58.     }  
  59.   
  60. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值