SpringMVC-Handler-Return Values返回值

Handler-Return Values返回值

支持的返回類型列表

Same in Spring WebFluxhtml

The table below shows supported controller method return values. Reactive types are supported for all return values, see below for more details.java

Controller method return valuereact

Descriptionios

@ResponseBodyweb

The return value is converted through HttpMessageConverters and written to the response. See @ResponseBody.spring

返回值經過HttpMessageConverters轉換並寫入響應。 請參閱@ResponseBody。編程

處理器功能處理方法的返回值做爲響應體(經過HttpMessageConverter進行類型轉換);json

做用: 該註解用於將Controller的方法返回的對象,經過適當的HttpMessageConverter轉換爲指定格式後,寫入到Response對象的body數據區。api

使用時機:返回的數據不是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指定響應結果:

一、使用request轉向頁面,以下:

request.getRequestDispatcher("頁面路徑").forward(request, response);

 

二、也能夠經過response頁面重定向:

response.sendRedirect("url")

 

三、也能夠經過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。因此轉發前請求的參數在轉發後仍然能夠讀取到。

帶域的返回

相關文章
相關標籤/搜索