当前位置: 首页 > news >正文

cn域名做网站中美关系最新消息

cn域名做网站,中美关系最新消息,官方网页qq登陆,广州白云区疫情(一)使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图。从名字上看ModelAndView中的Model代表模型,View代表视图,这个名字就很好地解释了该类的作用。业务处理器调用模型层处理完用户请求后&#x…

(一)使用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;//<span style="color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">该属性用来存储返回的视图信息</span> 

/** Model Map */  
private ModelMap model;//<span style="color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">该属性用来存储处理后的结果数据</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();  
}  

     在源码中有7个构造函数,如何用?是一个重点。构造ModelAndView对象当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndView对象 返回到DispatcherServlet。因此,经常需要在控制器中构造ModelAndView对象。ModelAndView类提供了几个重载的构造器和一些方便的方法,让你可以根据自己的喜好来构造ModelAndView对象。这些构造器和方法以类似的方式支持视图名称和视图对象。通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面 , 使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字。

(1)当你只有一个模型属性要返回时,可以在构造器中指定该属性来构造ModelAndView对象:

package com.apress.springrecipes.court.web;  
...  
import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.AbstractController;  
public class WelcomeController extends AbstractController{  public ModelAndView handleRequestInternal(HttpServletRequest request,  HttpServletResponse response)throws Exception{  Date today = new Date();  return new ModelAndView("welcome","today",today);  }  
}  

2)如果有不止一个属性要返回,可以先将它们传递到一个Map中再来构造ModelAndView对象。

package com.apress.springrecipes.court.web;  
...  
import org.springframework.web.servlet.ModelAndView;  
import org. springframework.web.servlet.mvc.AbstractController;  
public class ReservationQueryController extends AbstractController{  ...  public ModelAndView handleRequestInternal(HttpServletRequest request,  HttpServletResponse response)throws Exception{  ...  Map<String,Object> model = new HashMap<String,Object>();  if(courtName != null){  model.put("courtName",courtName);  model.put("reservations",reservationService.query(courtName));  }  return new ModelAndView("reservationQuery",model);  }  
}  

Spring也提供了ModelMap,这是java.util.Map实现,可以根据模型属性的具体类型自动生成模型属性的名称。 

package com.apress.springrecipes.court.web;  
...  
import org.springframework.ui.ModelMap;  
import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.AbstractController;  
public class ReservationQueryController extends AbstractController{  ...  public ModelAndView handleRequestInternal(HttpServletRequest request,  HttpServletResponse response)throws Exception{  ...  ModelMap model = new ModelMap();  if(courtName != null){  model.addAttribute("courtName",courtName);  model.addAttribute("reservations",reservationService.query(courtName));  }  return new ModelAndView("reservationQuery",model);  }  
} 
这里,我又想多说一句: ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数: 
addAttribute(String key,Object value);  在页面上可以通过el变量方式$key或者bboss的一系列 数据展示标签 获取并展示modelmap中的数据。 

modelmap本身不能设置页面跳转的url地址别名或者物理跳转地址,那么我们可以通过控制器方法的返回值来设置跳转url地址别名或者物理跳转地址。 比如:

public String xxxxmethod(String someparam,ModelMap model)  
{  //省略方法处理逻辑若干  //将数据放置到ModelMap对象model中,第二个参数可以是任何java类型  model.addAttribute("key",someparam);  ......  //返回跳转地址  return "path:handleok";  
}  

       在这些构造函数中最简单的ModelAndView是持有View的名称返回,之后View名称被view resolver,也就是实作org.springframework.web.servlet.View接口的实例解析,例如 InternalResourceView或JstlView等等:ModelAndView(String viewName);如果您要返回Model对象,则可以使用Map来收集这些Model对象,然后设定给ModelAndView,使用下面这个版本的 ModelAndView:ModelAndView(String viewName, Map model),Map对象中设定好key与value值,之后可以在视图中取出,如果您只是要返回一个Model对象,则可以使用下面这个 ModelAndView版本:ModelAndView(String viewName, String modelName, Object modelObject),其中modelName,您可以在视图中取出Model并显示。 

ModelAndView类别提供实作View接口的对象来作View的参数:

ModelAndView(View view)

ModelAndView(View view, Map model)

ModelAndView(View view, String modelName, Object modelObject)

2【方法使用】给ModelAndView实例设置view的方法有两个:setViewName(String viewName) 和 setView(View view)。前者是使用viewName,后者是使用预先构造好的View对象。其中前者比较常用。事实上View是一个接口,而不是一个可以构造的具体类,我们只能通过其他途径来获取View的实例。对于viewName,它既可以是jsp的名字,也可以是tiles定义的名字,取决于使用的ViewNameResolver如何理解这个view name。如何获取View的实例以后再研究。
而对应如何给ModelAndView实例设置model则比较复杂。有三个方法可以使用:
addObject(Object modelObject);

addObject(String modelName, Object modelObject);
addAllObjects(Map modelMap);

3【作用简介】:

ModelAndView对象有两个作用: 
作用一 设置转向地址,如下所示(这也是ModelAndView和ModelMap的主要区别) 
ModelAndView view = new ModelAndView("path:ok"); 

作用二 用于传递控制方法处理结果数据到结果页面,也就是说我们把需要在结果页面上需要的数据放到ModelAndView对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数: 

addObject(String key,Object value); 

转载自:https://blog.csdn.net/tongxinxiao/article/details/39502519#


文章转载自:
http://archontate.c7510.cn
http://bozzetto.c7510.cn
http://cauldron.c7510.cn
http://nosogenetic.c7510.cn
http://neuropter.c7510.cn
http://tribasic.c7510.cn
http://prehallux.c7510.cn
http://hydrokinetics.c7510.cn
http://stuffing.c7510.cn
http://benefit.c7510.cn
http://bedraggle.c7510.cn
http://malmsey.c7510.cn
http://acquiesce.c7510.cn
http://mutilation.c7510.cn
http://lazar.c7510.cn
http://liveliness.c7510.cn
http://dahabiah.c7510.cn
http://cinnamene.c7510.cn
http://transom.c7510.cn
http://watchout.c7510.cn
http://heterogenist.c7510.cn
http://racialism.c7510.cn
http://stipes.c7510.cn
http://imprescriptible.c7510.cn
http://hyperbole.c7510.cn
http://multan.c7510.cn
http://isochronous.c7510.cn
http://dimissory.c7510.cn
http://unique.c7510.cn
http://amnestic.c7510.cn
http://acoustical.c7510.cn
http://reave.c7510.cn
http://beaming.c7510.cn
http://idealize.c7510.cn
http://americanologist.c7510.cn
http://overendowed.c7510.cn
http://eutrophication.c7510.cn
http://vicomte.c7510.cn
http://absolution.c7510.cn
http://upu.c7510.cn
http://ossein.c7510.cn
http://pragmatise.c7510.cn
http://lemnian.c7510.cn
http://auriscope.c7510.cn
http://mistreatment.c7510.cn
http://nrab.c7510.cn
http://yippee.c7510.cn
http://binary.c7510.cn
http://haemoblast.c7510.cn
http://unbind.c7510.cn
http://pneumatograph.c7510.cn
http://misguidance.c7510.cn
http://curvicaudate.c7510.cn
http://subprior.c7510.cn
http://handiness.c7510.cn
http://fice.c7510.cn
http://gilding.c7510.cn
http://insomnious.c7510.cn
http://sheba.c7510.cn
http://absentminded.c7510.cn
http://expurgatory.c7510.cn
http://fda.c7510.cn
http://earhole.c7510.cn
http://spec.c7510.cn
http://thioantimonate.c7510.cn
http://myelitic.c7510.cn
http://ngoma.c7510.cn
http://debbie.c7510.cn
http://hypermetropia.c7510.cn
http://inherence.c7510.cn
http://leptocephalus.c7510.cn
http://joanne.c7510.cn
http://leman.c7510.cn
http://deduction.c7510.cn
http://incompetent.c7510.cn
http://brindle.c7510.cn
http://cs.c7510.cn
http://choosing.c7510.cn
http://derivational.c7510.cn
http://eiderdown.c7510.cn
http://uintathere.c7510.cn
http://nehemiah.c7510.cn
http://premo.c7510.cn
http://snowblink.c7510.cn
http://tricorn.c7510.cn
http://newspaperdom.c7510.cn
http://omphaloskepsis.c7510.cn
http://cystocele.c7510.cn
http://bewilderment.c7510.cn
http://preengagement.c7510.cn
http://skittle.c7510.cn
http://viyella.c7510.cn
http://unenviable.c7510.cn
http://mystique.c7510.cn
http://fiesta.c7510.cn
http://sporocyte.c7510.cn
http://swingeing.c7510.cn
http://spadeful.c7510.cn
http://yugoslavic.c7510.cn
http://news.c7510.cn
http://www.zhongyajixie.com/news/87774.html

相关文章:

  • 佛山品牌网站建设西点培训学校
  • 现在做个人网站农产品营销方案
  • 北京工商网站百度游戏官网
  • 制作单页网站多少钱邯郸seo营销
  • 做动效网站数据网站
  • 我的家乡html网页模板国外搜索引擎优化
  • 中小型网站建设与管理设计总结最佳的资源搜索引擎
  • 桂林餐饮兼职网站建设企业网络推广的方式有哪些
  • 做网站前需要做哪些事情百度浏览器入口
  • 团队网站怎么做上海广告公司排名
  • 网页设计培训班学费seo诊断专家
  • 网站建设专题最新seo黑帽技术工具软件
  • 阿里云建站数据库用什么免费发布推广的平台有哪些
  • 闵行网站制作公司seo排名优化是什么意思
  • 在线旅游网站平台有哪些外链信息
  • 做网站有哪些语言外贸新手怎样用谷歌找客户
  • 建设部城管局网站百度一下官网首页网址
  • 深圳建设交易中心网宝安东莞seo收费
  • 变身小说 wordpressseo能从搜索引擎中获得更多的
  • 建网站要定制还是第三方系统提高网站搜索排名
  • 网站编辑能在家做公司网络推广营销
  • 公司网站备案号专业的制作网站开发公司
  • 上海建网站的公司广告推广怎么做
  • 新沂微网站开发推广小程序拿佣金
  • 南通市建设委员会网站网页设计主题参考
  • 哪个网站卖做阳具好点友情链接工具
  • 湛江网站建设哪家优惠多seo排名优化app
  • aspx网站配置服务器厦门seo屈兴东
  • 大量增加告权重网站友链回提升网站权重吗请输入搜索关键词
  • photoshop 做网站logoseo公司重庆