本文主要是介绍Spring中ModelAndView源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring中ModelAndView源码
package org.springframework.web.servlet; import java.util.Map; import org.springframework.ui.ModelMap; import org.springframework.util.CollectionUtils; import org.springframework.web.servlet.View; public class ModelAndView {private Object view; private ModelMap model; private boolean cleared = false; public ModelAndView() {}public ModelAndView(String viewName) {this.view = viewName; }public ModelAndView(View view) {this.view = view; }public ModelAndView(String viewName, Map<String, ?> model) {this.view = viewName; if(model != null) {this.getModelMap().addAllAttributes(model); }}public ModelAndView(View view, Map<String, ?> model) {this.view = view; if(model != null) {this.getModelMap().addAllAttributes(model); }}public ModelAndView(String viewName, String modelName, Object modelObject) {this.view = viewName; this.addObject(modelName, modelObject); }public ModelAndView(View view, String modelName, Object modelObject) {this.view = view; this.addObject(modelName, modelObject); }public void setViewName(String viewName) {this.view = viewName; }public String getViewName() {return this.view instanceof String?(String)this.view:null; }public void setView(View view) {this.view = view; }public View getView() {return this.view instanceof View?(View)this.view:null; }public boolean hasView() {return this.view != null; }public boolean isReference() {return this.view instanceof String; }protected Map<String, Object> getModelInternal() {return this.model; }public ModelMap getModelMap() {if(this.model == null) {this.model = new ModelMap(); }return this.model; }public Map<String, Object> getModel() {return this.getModelMap(); }public ModelAndView addObject(String attributeName, Object attributeValue) {this.getModelMap().addAttribute(attributeName, attributeValue); return this; }public ModelAndView addObject(Object attributeValue) {this.getModelMap().addAttribute(attributeValue); return this; }public ModelAndView addAllObjects(Map<String, ?> modelMap) {this.getModelMap().addAllAttributes(modelMap); return this; }public void clear() {this.view = null; this.model = null; this.cleared = true; }public boolean isEmpty() {return this.view == null && CollectionUtils.isEmpty(this.model); }public boolean wasCleared() {return this.cleared && this.isEmpty(); }public String toString() {StringBuilder sb = new StringBuilder("ModelAndView: "); if(this.isReference()) {sb.append("reference to view with name \'").append(this.view).append("\'"); } else {sb.append("materialized View is [").append(this.view).append(']'); }sb.append("; model is ").append(this.model); return sb.toString(); } }
这篇关于Spring中ModelAndView源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!