Java @RequestMapping 方法中 spring mvc 使用哪种返回类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18607290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:28:18  来源:igfitidea点击:

Which return type use in spring mvc in @RequestMapping method?

javaspringspring-mvc

提问by

I know in spring mvc in @Controller class in @RequestMapping method I can return

我知道在@RequestMapping 方法中@Controller 类的spring mvc 中我可以返回

  1. String
  2. Model
  3. ModelAndView
  1. 细绳
  2. 模型
  3. 模型和视图

I don't understand differencies between these actions. Can you to explain me it?

我不明白这些行动之间的区别。你能给我解释一下吗?

采纳答案by MystyxMac

In Spring 3.2.x there are more then just those 3. See the docs at the Spring website. Latests Spring (4.2.x) documentation.

在 Spring 3.2.x 中,不止这 3 个。请参阅Spring 网站上的文档。最新 Spring (4.2.x)文档

The following are the supported return types:

以下是支持的返回类型:

  • A ModelAndViewobject, with the model implicitly enriched with command objects and the results of @ModelAttributeannotated reference data accessor methods.
  • A Modelobject, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttributeannotated reference data accessor methods.
  • A Mapobject for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttributeannotated reference data accessor methods.
  • A Viewobject, with the model implicitly determined through command objects and @ModelAttributeannotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • A Stringvalue that is interpreted as the logical view name, with the model implicitly determined through command objects and @ModelAttributeannotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • voidif the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).
  • If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters. See the section called “Mapping the response body with the @ResponseBodyannotation”.
  • A HttpEntity** or **ResponseEntityobject to provide access to the Servlet response HTTP headers and contents. The entity body will be converted to the response stream using HttpMessageConverters. See the section called “Using HttpEntity”.
  • A Callablecan be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC.
  • A DeferredResultcan be returned when the application wants to produce the return value from a thread of its own choosing.
  • Any other return type is considered to be a single model attribute to be exposed to the view, using the attribute name specified through @ModelAttributeat the method level (or the default attribute name based on the return type class name). The model is implicitly enriched with command objects and the results of @ModelAttributeannotated reference data accessor methods.
  • 一个ModelAndView对象,模型隐含地丰富了命令对象和带@ModelAttribute注释的引用数据访问器方法的结果。
  • 一个模型对象,其视图名称通过 RequestToViewNameTranslator 隐式确定,模型隐式丰富了命令对象和带@ModelAttribute注释的引用数据访问器方法的结果。
  • 地图对象用于曝光模式,与视图名称隐含地通过RequestToViewNameTranslator确定与模型隐含地命令对象和结果富集@ModelAttribute存取方法注释的参考数据。
  • 一个View对象,模型通过命令对象和带@ModelAttribute注释的引用数据访问器方法隐式确定。处理程序方法还可以通过声明模型参数(见上文)以编程方式丰富模型。
  • 解释为逻辑视图名称的字符串值,模型通过命令对象和带@ModelAttribute注释的引用数据访问器方法隐式确定。处理程序方法还可以通过声明模型参数(见上文)以编程方式丰富模型。
  • 空隙如果该方法处理反应本身(通过直接写入的响应内容,声明类型的ServletResponse的参数/ HttpServletResponse的用于此目的),或者如果视图名称应该通过RequestToViewNameTranslator来隐式地确定(未声明的响应参数处理程序方法签名)。
  • 如果该方法使用@ResponseBody 进行注释,则返回类型将写入响应 HTTP 正文。返回值将使用 HttpMessageConverters 转换为声明的方法参数类型。请参阅名为“使用@ResponseBody注释映射响应正文”的部分。
  • 一个HttpEntity** 或 **ResponseEntity对象,用于提供对 Servlet 响应 HTTP 标头和内容的访问。实体主体将使用 HttpMessageConverters 转换为响应流。请参阅“使用 HttpEntity”一节。
  • 一个可赎回可当应用程序需要由Spring MVC的管理线程异步产生的返回值返回。
  • 一个DeferredResult可以当应用程序想从它自己选择的线程产生的返回值返回。
  • 任何其他返回类型都被视为要公开给视图的单个模型属性,使用@ModelAttribute在方法级别指定的属性名称(或基于返回类型类名称的默认属性名称)。该模型隐含地丰富了命令对象和带@ModelAttribute注释的参考数据访问器方法的结果。

回答by Costi Ciudatu

Unless your return type is voidor you annotate your method with @ResponseBody, Spring MVC will try to resolve a Viewto render the response.

除非您的返回类型是void或您使用 注释您的方法,否则@ResponseBodySpring MVC 将尝试解析 aView以呈现响应。

Therefore, you must somehow point the framework to a Viewinstance or to the name of a view as Stringin your returned value (or rely on the implicit resolving and perhaps only return the Model); if you return a name (either as a mere Stringor embedded in a ModelAndView), that will then be passed to a configured ViewResolverto obtain an actual Viewinstance.

因此,您必须以某种方式将框架指向一个View实例或视图的名称,如String返回值(或依赖隐式解析,可能只返回Model);如果您返回一个名称(作为一个名称String或嵌入在 a 中ModelAndView),则该名称将被传递给配置ViewResolver以获取实际View实例。

The ModelAndViewcontainer does hold a reference to a Viewor view name and also embeds the model to use.

ModelAndView容器还是坚持一个参考View或视图名称,并嵌入到使用的模型。