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 ...
随机推荐
- LightOJ1245 Harmonic Number (II) —— 规律
题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II) PDF (English) Statistics ...
- HDU 5969 最大的位或 —— 贪心 + 二进制的理解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5969 最大的位或 Time Limit: 2000/1000 MS (Java/Others) ...
- java的一个爬虫
进行抓取页面,我看了一下人家的教程,一般要用到htmlparser用来解析html得到一个网页的相关链接,用httpclient抓取网页数据, 下面是一我写的spider类 package com.o ...
- plsql导入csv数据,未响应,invalid identifier
问题分析: 1.确保cvs字段名与表字段名一致,不要有空格 2.cvs字段对应表字段的大写,确保表字段都是大写 3.如果字段能对应上,plsql会自动识别出来
- HDU-2255(KM算法)
HDU-2255 题目意思转化之后就是,给你一个二分图(也称 二部图) ,要求选择一些边让左边的点都对应左边的某一个点!该问题也叫做二分图最大匹配.所以可以用KM算法来做这道题.KM前提你要理解匈牙利 ...
- CMD help
ASSOC 显示或修改文件扩展名关联. ATTRIB 显示或更改文件属性. BREAK 设置或清除扩展式 CTRL+C 检查. BCDEDIT 设置启动数据库中的属性以控制启动加载. CACLS 显示 ...
- BeginPaint/EndPaint(CPaintDC)与GetDC(CClientDC)的区别
在OnPaint函数中,用CClientDC dc(this)代替CPaintDC(this)后,界面不断闪烁. 说明:CClientDC是对GetDC的使用封装, CPaintDC是对BeginPa ...
- bzoj 4711 小奇挖矿 ——“承诺”类树形dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4711 对“承诺”有了更深的了解. 向外和向内要区分,所以 f [ i ][ j ] 表示根向 ...
- windows兼容dirent.h
尝试在windows下跑KCF算法,创建工程编译后出现: Error 4 error C1083: Cannot open include file: 'dirent.h': No such file ...
- Fuzzy Search
题意: 考虑模板串B和给定串A,给定K,对于模板串上给定位置j的字符,如果能在给定串上i左右K个字符内找到相同字符,则说可以匹配. 问有多少匹配. 解法: 考虑对于每一种字符分开求. 对于当前字符ch ...