在@RequestMapping方法中使用spring mvc中的哪种返回类型? [英] Which return type use in spring mvc in @RequestMapping method?

查看:137
本文介绍了在@RequestMapping方法中使用spring mvc中的哪种返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  1. 字符串

  2. 型号

  3. ModelAndView

我不明白这些操作之间的差异。
你能解释一下吗?

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

推荐答案

在Spring 3.2.x中,还有更多只有那些3.见春季网站。 Latests Spring(4.2.x)文档

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.

以下是支持的退货类型:

The following are the supported return types:


  • ModelAndView 对象,模型隐式地使用命令对象和 @ModelAttribute 带注释的引用数据访问器方法的结果。

  • 一个 Model 对象,通过RequestToViewNameTranslator隐式确定视图名称,并且模型隐式地使用命令对象和 @ModelAttribute的结果进行了丰富带注释的参考数据访问器方法。

  • 用于公开模型的 Map 对象,通过RequestToViewNameTranslator隐式确定视图名称模型隐式地丰富了命令对象和 @ModelAttribute 带注释的参考数据的结果ac cessor方法。

  • 一个 View 对象,模型通过命令对象隐式确定, @ModelAttribute 带注释参考数据访问器方法。处理程序方法还可以通过声明Model参数(参见上文)以编程方式丰富模型。

  • 一个 String 值,被解释为逻辑视图名称,模型通过命令对象隐式确定, @ModelAttribute 带注释的引用数据访问器方法。处理程序方法还可以通过声明Model参数(参见上文)以编程方式丰富模型。

  • void 如果方法处理响应本身(通过编写响应)直接内容,为此目的声明类型为ServletResponse / HttpServletResponse的参数)或者是否应该通过RequestToViewNameTranslator隐式确定视图名称(不在处理程序方法签名中声明响应参数)。

  • 如果方法使用 @ResponseBody 进行注释,则返回类型将写入响应HTTP正文。返回值将使用HttpMessageConverters转换为声明的方法参数类型。请参阅使用 @ResponseBody 注释映射响应正文一节。

  • A HttpEntity **或** ResponseEntity 对象,提供对Servlet响应HTTP标头和内容的访问。使用HttpMessageConverters将实体主体转换为响应流。请参阅使用HttpEntity一节。

  • 当应用程序想要在Spring MVC管理的线程中异步生成返回值时,可以返回 Callable

  • 当应用程序想要从自己选择​​的线程生成返回值时,可以返回 DeferredResult

  • 使用在方法级别通过 @ModelAttribute 指定的属性名称(或默认属性),将任何其他返回类型视为要向视图公开的单个模型属性名称基于返回类型类名称)。该模型隐含地使用命令对象和 @ModelAttribute 带注释的引用数据访问器方法的结果进行了丰富。

  • A ModelAndView object, with the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Model object, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Map object 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 @ModelAttribute annotated reference data accessor methods.
  • A View object, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • A String value that is interpreted as the logical view name, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • void if 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 @ResponseBody annotation".
  • A HttpEntity** or **ResponseEntity object 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 Callable can be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC.
  • A DeferredResult can 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 @ModelAttribute at 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 @ModelAttribute annotated reference data accessor methods.

这篇关于在@RequestMapping方法中使用spring mvc中的哪种返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆