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 ...
随机推荐
- Linux ARM交叉编译工具链制作过程【转】
本文转载自:http://www.cnblogs.com/Charles-Zhang-Blog/archive/2013/02/21/2920999.html 一.下载源文件 源代码文件及其版本与下载 ...
- H3C-交换机密码恢复
交换机密码恢复: 一. 拔掉电源再插上重新启动交换机,在超级终端中可以看到交换机启动画面,当出现提示按CTRL+B时,此时按住CTRL+B,我们会看到有9个选项: 1. download applic ...
- 静态路由配置及RIP配置实验
[实验环境] Packet Trace 5.3 模拟软件. [实验步骤] 1.首先要进行IP地址规划.(例如下图格式) 网络名 网络地址 子网掩码 网关 主机IP vlan1 10.10.1.0 25 ...
- PS基本操作
1 安装 赢政天下2015大师版 安装失败, 删除一下文件夹再重新安装 2 工作界面 2.1 界面 菜单栏; 标题栏; 工具箱; 工具箱选项栏; 面板; 状态栏; 文档窗口; 选项卡 2.2 文档窗口 ...
- 无向图hash
一个效果还行的 无向图hash判同构的方法 求出每个点向其它点的最短路,然后排序,然后按字符串拼接起来,再按每个点的字符串 排序后的rank 作为每一个点的初始hash值 然后每一轮,把每个点的相邻点 ...
- 内存泄漏分析工具tMemMonitor (TMM)使用简介
C/C++由于灵活.高效的优点一直以来都是主流的程序设计语言之一,但是其内存的分配与释放均由程序员自己管理,当由于疏忽或错误造成程序未能释放不再使用的内存时就会造成内存泄漏.在大型.复杂的应用程序中, ...
- YouTube视频签名加密算法的破解
密码学方法 多年以前,YouTube的视频源地址是直接encode在页面中的,你甚至可以用一行Perl来下载它们. 直到2012年8月,这个简单的脚本(用在0.0.1版本的You-Get中)仍然可以解 ...
- bzoj4006
斯坦纳树 比之前要求高了一些 其实利用斯坦纳树的dp[i][s]以i为根,S为状态就行了,先跑一遍斯坦纳树,预处理出dp数组,记住每个S的最小值,然后再dp,这里dp必须要求同一种颜色的状态都必须在S ...
- Linux系统如何查看版本信息?
查看版本号 我在Ubuntu下做测试 1 命令行执行 cat /etc/issue (切记cat后要空一格)即可看到版本信息. 2 登录linux,在终端输入 cat /proc/version ...
- c/c++面试39-44之内存动态分配
39 指针的初始化(二叉树排序),其中引入了双向链表 #include <stdio.h> #include <stdlib.h> struct Tag_Node { stru ...