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

前端 未结 3 1208
一整个雨季
一整个雨季 2020-12-09 05:36

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

  1. String
  2. Model
  3. ModelAndView

I don\'t understa

相关标签:
3条回答
  • 2020-12-09 06:07

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

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

    The ModelAndView container does hold a reference to a View or view name and also embeds the model to use.

    0 讨论(0)
  • 2020-12-09 06:09

    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:

    • 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.
    0 讨论(0)
  • 2020-12-09 06:12

    Everything is in the documentation: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-return-types

    0 讨论(0)
提交回复
热议问题