SpringMVC-Handler-Return Values返回值
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。所以转发前请求的参数在转发后仍然可以读取到。
带域的返回
SpringMVC-Handler-Return Values返回值的更多相关文章
- SSM框架之SpringMVC(4)返回值类型及响应数据类型
SpringMVC(4)返回值类型及响应数据类型 1. 返回值分类 1.1. 返回字符串 Controller方法返回字符串可以指定逻辑视图的名称,根据视图解析器为物理视图的地址. @RequestM ...
- return 的返回值与结束功能
前言:大家好~我是阿飞~在js中return是很重要的基础.一定要彻底掌握理解它哦.否则js学习到中后期很容易懵逼的+_+ 什么是return? 1.在js中return是一个表达式语句,如果后面什么 ...
- defer、return、返回值,这三者的执行逻辑
defer.return.返回值,这三者的执行逻辑是: return 最先执行,return 负责将结果写入返回值中:接着defer执行,可能修改返回值:最后函数携带当前返回值退出.
- go中defer的理解--defer、return、返回值之间执行顺序
defer可以读取有名返回值 func c() (i int) { defer func() { i++ }() return 1 } 输出结果是2. 在开头的时候,我们知道defer是在return ...
- Golang中defer、return、返回值之间执行顺序的坑
原文链接:https://studygolang.com/articles/4809 Go语言中延迟函数defer充当着 cry...catch 的重任,使用起来也非常简便,然而在实际应用中,很多go ...
- 获取的ajax方法return的返回值的问题解析
今天刚上班就偶遇关于获取Ajax方法return的返回值的问题,这里小记一下. 在使用jquery中,如果获取不到ajax返回值,原因有二: 一.ajax未使用同步 ajax未使用同步,导致数据未加载 ...
- SpringMVC(二)返回值设置、数据在域中的保存与SpringMVC案例
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.返回值的设置 1.返回 String [1]返回 String 默认情况 @RequestMappi ...
- SpringMVC中 controller方法返回值
1)ModelAndView @RequestMapping(value="/itemEdit") public ModelAndView itemEdit(){ //创建模型视图 ...
- jquery中ajax用return来返回值无效
jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...
随机推荐
- LightOJ1234 Harmonic Number —— 分区打表
题目链接:https://vjudge.net/problem/LightOJ-1234 1234 - Harmonic Number PDF (English) Statistics Foru ...
- PYTHON 爬虫笔记四:正则表达式基础用法
知识点一:正则表达式详解及其基本使用方法 什么是正则表达式 正则表达式对子符串操作的一种逻辑公式,就是事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来表达 ...
- html5--5-9 绘制扇形
html5--5-9 绘制扇形 学习要点 综合运用已经学过的知识绘制一个扇形 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y,w,h) 绘制矩形(无 ...
- Ueditor文本编辑工具栏自定义
1.找到 ueditor.config.js 文件 2.在 toolbars 数组内进行删减,添加 , toolbars: [[ 'fullscreen', 'source', '|', 'undo' ...
- 【转】看C++文档的小知识
转载:http://www.ggv.com.cn/forum/clib/ctype/isspace.html 函数isspace 原型:extern int isspace(int c); 用法 ...
- LaTeX常用的符号
推荐新手使用的网站:http://latex.codecogs.com/eqneditor/editor.php \(\sum _{d|n}{u(d)F(\frac{n}{d})}\) \sum _{ ...
- 关于yolo 模型中1X1卷积层的作用
1X1卷积层的作用: 1.实现跨通道的交互和信息整合.2.进行卷积核通道数的降维和升维.3.就是可以在保持feature map 尺寸不变(即不损失分辨率)的前提下大幅增加非线性特性,把网络做得很de ...
- C++模板之函数模板实例化和具体化
模板声明 template<typename/class T>, typename比class最近后添加到C++标准. 常规模板,具体化模板,非模板函数的优先调用顺序. 非模板函数(普通 ...
- js遍历checkbox获取数据
function GetCurrenetPoint() { debugger var currentTypes = ""; var display = $("#input ...
- CSS元素:clip属性作用说明
clip属性是一个比较有用的属性,但往往在实际应用中,并不多见,介绍的也很少.应用clip属性需要注意的两点: 一.clip属性必须和定位属性postion一起使用才能生效. 二.clip裁切的计算坐 ...