Handler-Return Values返回值
支持的返回类型列表
The table below shows supported controller method return values. Reactive types are supported for all return values, see below for more details.
Controller method return value |
Description |
@ResponseBody |
The return value is converted through HttpMessageConverters and written to the response. See @ResponseBody. 返回值通过HttpMessageConverters转换并写入响应。 请参阅@ResponseBody。 处理器功能处理方法的返回值作为响应体(通过HttpMessageConverter进行类型转换); 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。 使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用; |
HttpEntity<B>, ResponseEntity<B> |
The return value specifies the full response including HTTP headers and body be converted through HttpMessageConverters and written to the response. See ResponseEntity. 返回值指定完整响应,包括HTTP标头和正文通过HttpMessageConverters转换并写入响应。 请参阅ResponseEntity。 |
HttpHeaders |
For returning a response with headers and no body. 为了返回一个响应头和没有正文。 |
String |
A view name to be resolved with ViewResolver's and used together with the implicit model — determined through command objects and @ModelAttribute methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above). 一个视图名称,用ViewResolver解决,并与隐式模型一起使用 - 通过命令对象和@ModelAttribute方法确定。 处理程序方法也可以通过声明一个Model参数来以编程方式丰富模型(参见上文)。 |
View |
A View instance to use for rendering together with the implicit model — determined through command objects and @ModelAttribute methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above). 用于与隐式模型一起渲染的View实例 - 通过命令对象和@ModelAttribute方法确定。 处理程序方法也可以通过声明一个Model参数来以编程方式丰富模型(参见上文)。 |
java.util.Map, org.springframework.ui.Model |
Attributes to be added to the implicit model with the view name implicitly determined through a RequestToViewNameTranslator. 要通过RequestToViewNameTranslator隐式确定的视图名称添加到隐式模型的属性。 |
@ModelAttribute |
An attribute to be added to the model with the view name implicitly determined through a RequestToViewNameTranslator. Note that @ModelAttribute is optional. See "Any other return value" further below in this table. 要通过RequestToViewNameTranslator隐式确定的视图名称添加到模型的属性。 请注意@ModelAttribute是可选的。 请参阅本表下面的“任何其他返回值”。 |
ModelAndView object |
The view and model attributes to use, and optionally a response status. 要使用的视图和模型属性,以及可选的响应状态。 |
void |
A method with a void return type (or null return value) is considered to have fully handled the response if it also has a ServletResponse, or an OutputStream argument, or an @ResponseStatus annotation. The same is true also if the controller has made a positive ETag or lastModified timestamp check (see @Controller caching for details). If none of the above is true, a void return type may also indicate "no response body" for REST controllers, or default view name selection for HTML controllers. 具有void返回类型(或返回值为null)的方法如果还有ServletResponse,OutputStream参数或@ResponseStatus注释,则认为它已完全处理响应。 如果控制器进行了积极的ETag或lastModified时间戳检查(请参阅@Controller缓存了解详细信息),情况也是如此。 如果以上都不是这样,那么void返回类型也可能指示REST控制器的“无响应主体”,或HTML控制器的默认视图名称选择。 |
DeferredResult<V> |
Produce any of the above return values asynchronously from any thread — e.g. possibly as a result of some event or callback. See Async Requests and DeferredResult. 从任何线程异步生成任何上述返回值 - 例如 可能是由于某些事件或回调。 请参阅异步请求和延期结果。 |
Callable<V> |
Produce any of the above return values asynchronously in a Spring MVC managed thread. See Async Requests and Callable. |
ListenableFuture<V>, java.util.concurrent.CompletionStage<V>, java.util.concurrent.CompletableFuture<V> |
Alternative to DeferredResult as a convenience for example when an underlying service returns one of those. 在Spring MVC托管线程中异步生成上述任何返回值。 请参阅异步请求和可调用 |
ResponseBodyEmitter, SseEmitter |
Emit a stream of objects asynchronously to be written to the response with HttpMessageConverter's; also supported as the body of a ResponseEntity. See Async Requests and HTTP Streaming. 用HttpMessageConverter's异步发出一个对象流写入响应; 也支持作为ResponseEntity的主体。 请参阅异步请求和HTTP流。 |
StreamingResponseBody |
Write to the response OutputStream asynchronously; also supported as the body of a ResponseEntity. See Async Requests and HTTP Streaming. 异步写入响应的OutputStream; 也支持作为ResponseEntity的主体。请参阅异步请求和HTTP流程。 |
Reactive types — Reactor, RxJava, or others via ReactiveAdapterRegistry |
Alternative to `DeferredResult with multi-value streams (e.g. Flux, Observable) collected to a List. For streaming scenarios — e.g. text/event-stream, application/json+stream — SseEmitter and ResponseBodyEmitter are used instead, where ServletOutputStream blocking I/O is performed on a Spring MVC managed thread and back pressure applied against the completion of each write. See Async Requests and Reactive types. 具有多值流的DeferredResult(例如Flux,Observable)的替代方法被收集到列表中。 对于流式场景 - 例如 text / event-stream,application / json + stream - 使用SseEmitter和ResponseBodyEmitter,而在Spring MVC托管线程上执行ServletOutputStream阻塞I / O,并在每次写入完成时施加背压。 请参阅异步请求和反应类型。 |
Any other return value |
If a return value is not matched to any of the above, by default it is treated as a view name, if it is String or void (default view name selection via RequestToViewNameTranslator applies); or as a model attribute to be added to the model, unless it is a simple type, as determined by BeanUtils#isSimpleProperty in which case it remains unresolved. 如果返回值与以上任何一个不匹配,默认情况下它被视为视图名称,如果它是String或void(通过RequestToViewNameTranslator应用的默认视图名称选择); 或者作为要添加到模型的模型属性,除非它是一个简单的类型,由BeanUtils#isSimpleProperty确定,在这种情况下,它仍然未解决。 |
@返回ModelAndView
controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。
@返回void
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
1、使用request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);
2、也可以通过response页面重定向:
response.sendRedirect("url")
3、也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
@返回字符串
1, 返回void类型,使用response返回
2, 使用ResponseBody注解,返回String字符串
3, 在类上面加入RestResponse返回String字符串
@RestController
public class TestController {
@RequestMapping("/test.action")
public String test( int ids){
System.out.println("id:"+ids);
return "this is test";
}
}
@逻辑视图名
controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。
//指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/item/editItem.jsp
return "item/editItem";
@Redirect重定向
Contrller方法返回结果重定向到一个url地址,如下商品修改提交后重定向到商品查询方法,参数无法带到商品查询方法中。
//重定向到queryItem.action地址,request无法带过去
return "redirect:queryItem.action";
redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。
由于新发起一个request原来的参数在转发时就不能传递到下一个url,如果要传参数可以/item/queryItem.action后边加参数,如下:
/item/queryItem?...&…..
对于model设置的值,重定向会拼接到
@forward转发
controller方法执行后继续执行另一个controller方法,如下商品修改提交后转向到商品修改页面,修改商品的id参数可以带到商品修改方法中。
//结果转发到editItem.action,request可以带过去
return "forward:editItem.action";
forward方式相当于“request.getRequestDispatcher().forward(request,response)”,转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。
带域的返回