国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

SpringMVC ModelAndView的用法使用詳解

2024-07-09 22:42:18
字體:
來源:轉載
供稿:網友

(一)使用ModelAndView類用來存儲處理完后的結果數據,以及顯示該數據的視圖。從名字上看ModelAndView中的Model代表模型,View代表視圖,這個名字就很好地解釋了該類的作用。業務處理器調用模型層處理完用戶請求后,把結果數據存儲在該類的model屬性中,把要返回的視圖信息存儲在該類的view屬性中,然后讓該ModelAndView返回該Spring MVC框架。框架通過調用配置文件中定義的視圖解析器,對該對象進行解析,最后把結果數據顯示在指定的頁面上。

具體作用:

1、返回指定頁面

ModelAndView構造方法可以指定返回的頁面名稱,

也可以通過setViewName()方法跳轉到指定的頁面 ,

2、返回所需數值

使用addObject()設置需要返回的值,addObject()有幾個不同參數的方法,可以默認和指定返回對象的名字。

1、【其源碼】:熟悉一個類的用法,最好從其源碼入手。

public class ModelAndView {    /** View instance or view name String */   private Object view //該屬性用來存儲返回的視圖信息/** Model Map */ private ModelMap model;//<span >該屬性用來存儲處理后的結果數據</span>  /**  * Indicates whether or not this instance has been cleared with a call to {@link #clear()}.  */ private boolean cleared = false;   /**  * Default constructor for bean-style usage: populating bean  * properties instead of passing in constructor arguments.  * @see #setView(View)  * @see #setViewName(String)  */ public ModelAndView() { }  /**  * Convenient constructor when there is no model data to expose.  * Can also be used in conjunction with <code>addObject</code>.  * @param viewName name of the View to render, to be resolved  * by the DispatcherServlet's ViewResolver  * @see #addObject  */ public ModelAndView(String viewName) {   this.view = viewName; }  /**  * Convenient constructor when there is no model data to expose.  * Can also be used in conjunction with <code>addObject</code>.  * @param view View object to render  * @see #addObject  */ public ModelAndView(View view) {   this.view = view; }  /**  * Creates new ModelAndView given a view name and a model.  * @param viewName name of the View to render, to be resolved  * by the DispatcherServlet's ViewResolver  * @param model Map of model names (Strings) to model objects  * (Objects). Model entries may not be <code>null</code>, but the  * model Map may be <code>null</code> if there is no model data.  */ public ModelAndView(String viewName, Map<String, ?> model) {   this.view = viewName;   if (model != null) {     getModelMap().addAllAttributes(model);   } }  /**  * Creates new ModelAndView given a View object and a model.  * <emphasis>Note: the supplied model data is copied into the internal  * storage of this class. You should not consider to modify the supplied  * Map after supplying it to this class</emphasis>  * @param view View object to render  * @param model Map of model names (Strings) to model objects  * (Objects). Model entries may not be <code>null</code>, but the  * model Map may be <code>null</code> if there is no model data.  */ public ModelAndView(View view, Map<String, ?> model) {   this.view = view;   if (model != null) {     getModelMap().addAllAttributes(model);   } }  /**  * Convenient constructor to take a single model object.  * @param viewName name of the View to render, to be resolved  * by the DispatcherServlet's ViewResolver  * @param modelName name of the single entry in the model  * @param modelObject the single model object  */ public ModelAndView(String viewName, String modelName, Object modelObject) {   this.view = viewName;   addObject(modelName, modelObject); }  /**  * Convenient constructor to take a single model object.  * @param view View object to render  * @param modelName name of the single entry in the model  * @param modelObject the single model object  */ public ModelAndView(View view, String modelName, Object modelObject) {   this.view = view;   addObject(modelName, modelObject); }   /**  * Set a view name for this ModelAndView, to be resolved by the  * DispatcherServlet via a ViewResolver. Will override any  * pre-existing view name or View.  */ public void setViewName(String viewName) {   this.view = viewName; }  /**  * Return the view name to be resolved by the DispatcherServlet  * via a ViewResolver, or <code>null</code> if we are using a View object.  */ public String getViewName() {   return (this.view instanceof String ? (String) this.view : null); }  /**  * Set a View object for this ModelAndView. Will override any  * pre-existing view name or View.  */ public void setView(View view) {   this.view = view; }  /**  * Return the View object, or <code>null</code> if we are using a view name  * to be resolved by the DispatcherServlet via a ViewResolver.  */ public View getView() {   return (this.view instanceof View ? (View) this.view : null); }  /**  * Indicate whether or not this <code>ModelAndView</code> has a view, either  * as a view name or as a direct {@link View} instance.  */ public boolean hasView() {   return (this.view != null); }  /**  * Return whether we use a view reference, i.e. <code>true</code>  * if the view has been specified via a name to be resolved by the  * DispatcherServlet via a ViewResolver.  */ public boolean isReference() {   return (this.view instanceof String); }  /**  * Return the model map. May return <code>null</code>.  * Called by DispatcherServlet for evaluation of the model.  */ protected Map<String, Object> getModelInternal() {   return this.model; }  /**  * Return the underlying <code>ModelMap</code> instance (never <code>null</code>).  */ public ModelMap getModelMap() {   if (this.model == null) {     this.model = new ModelMap();   }   return this.model; }  /**  * Return the model map. Never returns <code>null</code>.  * To be called by application code for modifying the model.  */ public Map<String, Object> getModel() {   return getModelMap(); }   /**  * Add an attribute to the model.  * @param attributeName name of the object to add to the model  * @param attributeValue object to add to the model (never <code>null</code>)  * @see ModelMap#addAttribute(String, Object)  * @see #getModelMap()  */ public ModelAndView addObject(String attributeName, Object attributeValue) {   getModelMap().addAttribute(attributeName, attributeValue);   return this; }  /**  * Add an attribute to the model using parameter name generation.  * @param attributeValue the object to add to the model (never <code>null</code>)  * @see ModelMap#addAttribute(Object)  * @see #getModelMap()  */ public ModelAndView addObject(Object attributeValue) {   getModelMap().addAttribute(attributeValue);   return this; }  /**  * Add all attributes contained in the provided Map to the model.  * @param modelMap a Map of attributeName -> attributeValue pairs  * @see ModelMap#addAllAttributes(Map)  * @see #getModelMap()  */ public ModelAndView addAllObjects(Map<String, ?> modelMap) {   getModelMap().addAllAttributes(modelMap);   return this; }   /**  * Clear the state of this ModelAndView object.  * The object will be empty afterwards.  * <p>Can be used to suppress rendering of a given ModelAndView object  * in the <code>postHandle</code> method of a HandlerInterceptor.  * @see #isEmpty()  * @see HandlerInterceptor#postHandle  */ public void clear() {   this.view = null;   this.model = null;   this.cleared = true; }  /**  * Return whether this ModelAndView object is empty,  * i.e. whether it does not hold any view and does not contain a model.  */ public boolean isEmpty() {   return (this.view == null && CollectionUtils.isEmpty(this.model)); }  /**  * Return whether this ModelAndView object is empty as a result of a call to {@link #clear}  * i.e. whether it does not hold any view and does not contain a model.  * <p>Returns <code>false</code> if any additional state was added to the instance  * <strong>after</strong> the call to {@link #clear}.  * @see #clear()  */ public boolean wasCleared() {   return (this.cleared && isEmpty()); }   /**  * Return diagnostic information about this model and view.  */ @Override public String toString() {   StringBuilder sb = new StringBuilder("ModelAndView: ");   if (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(); } 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉门市| 瑞昌市| 合川市| 巴里| 南昌县| 乌拉特后旗| 错那县| 云和县| 郴州市| 含山县| 乌拉特前旗| 汝南县| 南皮县| 阿合奇县| 镇安县| 勐海县| 新泰市| 武乡县| 始兴县| 乐安县| 清水河县| 崇阳县| 广南县| 佛学| 富锦市| 阿拉尔市| 石台县| 云林县| 依兰县| 胶南市| 抚顺县| 江阴市| 墨竹工卡县| 阳城县| 平舆县| 贡觉县| 新平| 清苑县| 彰化县| 青龙| 新昌县|