SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes、model.addFlashAttribute("spitter", spitter);)
一、redirect为什么会丢数据?
when a handler method completes, any model data specified in the method is copied into the request as request attributes, and the request is forwarded to the view for rendering. Because it’s the same request that’s handled by both
the controller method and the view, the request attributes survive the forward.But as illustrated in figure 7.1, when a controller method results in a redirect, the original request ends and a new HTTP GET request begins. Any model data carried in the original request dies with the request. The new request is devoid of any model data in its attributes and has to figure it out on its own.Clearly, the model isn’t going to help you carry data across a redirect. But there are
a couple of options to get the data from the redirecting method to the redirect handling method:
Passing data as path variables and/or query parameters using URL templates
Sending data in flash attributes
二、拼url字符串
- return "redirect:/spitter/" + spitter.getUsername();
三、用model和占位符
在方法中加上model参数,model中的数据能匹配占位符的就作为path参数,否则会作为query参数,如
- @RequestMapping(value = "/register", method = POST)
- public String processRegistration(
- Spitter spitter, Model model) {
- spitterRepository.save(spitter);
- model.addAttribute("username", spitter.getUsername());
- model.addAttribute("spitterId", spitter.getId());
- return "redirect:/spitter/{username}";
- }
占位符会编码,把直接拼接字符串安全些,Because it’s filled into the placeholder in the URL template instead of concatenated
into the redirect String , any unsafe characters in the username property are escaped.This is safer than allowing the user to type in whatever they want for the username and then appending it to the path.
因为id匹配不到占位符,假设 username attribute is habuma and the spitterId attribute is 42 , then the resulting redirect path will be /spitter/habuma?spitterId=42
三、用flash attributes->RedirectAttributes
1.需求:
Let’s say that instead of sending a username or ID in the redirect, you want to send the actual Spitter object. If you send just the ID , then the method that handles the redirect has to turn around and look up the Spitter from the database. But before the redirect, you already have the Spitter object in hand. Why not send it to the redirect-handling method to display?
2.
RedirectAttributes是model的子接口,工作原理是用session,Before the redirect takes place, all flash attributes are copied into the session. After the redirect, the flash attributes stored in the session are moved out of the session and into the model. The method that handles the redirect request can then access the Spitter from the model, just like any other model object. Figure 7.2 illustrates how this works.
3.代码实现
- @RequestMapping(value = "/register", method = POST)
- public String processRegistration(
- Spitter spitter, RedirectAttributes model) {
- spitterRepository.save(spitter);
- model.addAttribute("username", spitter.getUsername());
- model.addFlashAttribute("spitter", spitter);
- return "redirect:/spitter/{username}";
- }
- @RequestMapping(value = "/{username}", method = GET)
- public String showSpitterProfile(
- @PathVariable String username, Model model) {
- if (!model.containsAttribute("spitter")) {
- model.addAttribute(
- spitterRepository.findByUsername(username));
- }
- return "profile";
- }
SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes、model.addFlashAttribute("spitter", spitter);)的更多相关文章
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
No matter what happens, good or bad, the outcome of a servlet request is a servlet response. If an e ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver
一.什么是multipart The Spittr application calls for file uploads in two places. When a new user register ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-004- 处理上传文件
一.用 MultipartFile 1.在html中设置<form enctype="multipart/form-data">及<input type=&quo ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynamic、WebApplicationInitializer)
一. 1.如想在DispatcherServlet在Servlet容器中注册后自定义一些操作,如开启文件上传功能,则可重写通过AbstractAnnotationConfigDispatcherSer ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-007-表单验证@Valid、Error
一. Starting with Spring 3.0, Spring supports the Java Validation API in Spring MVC . No extra config ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)
一 1.以path parameters的形式给action传参数 @Test public void testSpittle() throws Exception { Spittle expecte ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model
一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 package spittr.web; import static org.springframework.web.bind ...
随机推荐
- ASP连接MYSQL数据库
<% strconnection="driver={mysql odbc 3.51 driver};database=weste_net;server=localhost;uid=ro ...
- sQL语言分类 DML、DDL、DCL区别
总体解释:DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据 ...
- WCF编程系列(七)信道及信道工厂
WCF编程系列(七)信道及信道工厂 信道及信道栈 前面已经提及过,WCF中客户端与服务端的交互都是通过消息来进行的.消息从客户端传送到服务端会经过多个处理动作,在WCF编程模型中,这些动作是按层 ...
- (转)安装程序发布利器——InstallShield 2011 Limited Edition
最近经常写WCF服务和Windows服务,之前知道可以通过vs2010自带的“安装项目”可以发布程序,但是自vs2010起,同时提供了InstallShield LE. 下面我们通过图示,来了解Ins ...
- 在Linux系统下安装大于mysql5.5版本的数据库
linux下mysql 5.5的安装方法: 1.安装所需要系统库相关库文件 gcc等开发包,在安装linux系统的时候安装. 2.创建mysql安装目录 # mkdir -p /usr/lo ...
- HTML5新增标签属性
----- 新类型表单 - email 自动校验输入的是不否是email 邮箱:<input type="email" name="user_email" ...
- linux shell编程学习笔记(二) --- grep命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- docker+tomcat+java配置(ubuntu)
原创文章,转载请注明出处. 本文是基于ubuntu14.04搭建的dosker,tomcat配置是在docker容器外面(为了方便查看日志)使用的是docker启动挂载,该tomcat工程依赖于doc ...
- 重新开始学习c#啦,希望能坚持下去!
过了这么多年,还是感觉自己喜欢C#,喜欢编程,虽然自己什么技术也没有:做的项目也不算是项目:
- DIV CSS布局中position属性用法深入探究
本文向大家描述一下DIV CSS布局中的position属性的用法,position属性主要有四种属性值,任何元素的默认position的属性值均是static,静态.这节课主要讲讲relative( ...