pmalex 2009-03-20 11:47
浏览 841
已采纳

spring mvc为何不推荐使用addObject方法?

我用的spring mvc开发的值钱项目使用的ModelAndView的方式返回的参数,和映射地址!

现在开发使用了spring 2.5用上了批注!返回值的传递用ModelMap。
为何原来的ModelAndView 支持addObject,但是ModelMap的这个方法就花了个横杠?
我知道是过期的意思,但是我想知道处于何目的!

(以后要是遇到同样问题如何去找,答案,希望深入了解下!)
[b]问题补充:[/b]
确实有,希望有人能解答一下!

  • 写回答

2条回答

  • iteye_17981 2009-03-20 14:01
    关注

    读了下源代码,确实没有.
    不知道你的spring到底是哪个版本.
    下面是2.5.6的ModelAndView的代码:
    /*

    • Copyright 2002-2006 the original author or authors. *
    • Licensed under the Apache License, Version 2.0 (the "License");
    • you may not use this file except in compliance with the License.
    • You may obtain a copy of the License at *
    • http://www.apache.org/licenses/LICENSE-2.0 *
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS,
    • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    • See the License for the specific language governing permissions and
    • limitations under the License. */

    package org.springframework.web.servlet;

    import java.util.Map;

    import org.springframework.ui.ModelMap;

    /**

    • Holder for both Model and View in the web MVC framework.
    • Note that these are entirely distinct. This class merely holds
    • both to make it possible for a controller to return both model
    • and view in a single return value. *
    • Represents a model and view returned by a handler, to be resolved

    • by a DispatcherServlet. The view can take the form of a String
    • view name which will need to be resolved by a ViewResolver object;
    • alternatively a View object can be specified directly. The model
    • is a Map, allowing the use of multiple objects keyed by name. *
    • @author Rod Johnson
    • @author Juergen Hoeller
    • @author Rob Harrop
    • @see DispatcherServlet
    • @see ViewResolver
    • @see HandlerAdapter#handle
    • @see org.springframework.web.servlet.mvc.Controller#handleRequest
      */
      public class ModelAndView {

      /** View instance or view name String */
      private Object view;

      /** Model Map */
      private ModelMap model;

      /**

      • Indicates whether or not this instance has been cleared with a call to {@link #clear()}. */ private boolean cleared;

      /**

      • 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 addObject.
      • @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 addObject.
      • @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 null, but the
      • model Map may be null if there is no model data. */ public ModelAndView(String viewName, Map model) { this.view = viewName; if (model != null) { getModelMap().addAllAttributes(model); } }

      /**

      • Creates new ModelAndView given a View object and a model.
      • 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
      • @param view View object to render
      • @param model Map of model names (Strings) to model objects
      • (Objects). Model entries may not be null, but the
      • model Map may be null if there is no model data. */ public ModelAndView(View view, Map 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 null 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 null 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 ModelAndView 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. true
      • 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 null.
      • Called by DispatcherServlet for evaluation of the model. */ protected Map getModelInternal() { return this.model; }

      /**

      • Return the underlying ModelMap instance (never null). */ public ModelMap getModelMap() { if (this.model == null) { this.model = new ModelMap(); } return this.model; }

      /**

      • Return the model map. Never returns null.
      • To be called by application code for modifying the model. */ public Map 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 null)
      • @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 null)
      • @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 modelMap) { getModelMap().addAllAttributes(modelMap); return this; }

      /**

      • Clear the state of this ModelAndView object.
      • The object will be empty afterwards.
      • Can be used to suppress rendering of a given ModelAndView object

      • in the postHandle 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 && this.model == null); }

      /**

      • 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.
      • Returns false if any additional state was added to the instance
      • after the call to {@link #clear}.
      • @see #clear() */ public boolean wasCleared() { return (this.cleared && isEmpty()); }

      /**

      • Return diagnostic information about this model and view. */ public String toString() { StringBuffer buf = new StringBuffer("ModelAndView: "); if (isReference()) { buf.append("reference to view with name '").append(this.view).append("'"); } else { buf.append("materialized View is [").append(this.view).append(']'); } buf.append("; model is ").append(this.model); return buf.toString(); } }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题