第7章 Spring MVC的高级技术---跨重定向请求传递数据
来源:互联网 发布:农村淘宝推广员 编辑:程序博客网 时间:2024/06/06 03:00
概述:
大家都知道重定向和转发的区别,重定向其实两个请求,而转发是一次请求,在请求一次之后,该请求的数据会被销毁,现在问题是:
需要重定向的功能而且希望第一次请求的数据能复制到下一次请求中?如何实现呢?
有两种方式:
- 使用URL模板以路径变量和/或查询参数的形式传递数据(缺点就是有长度现在,传递数据类型单一)
- 通过flash属性发送数据(其实底层就是session技术)
1、使用URL模板进行重定向
方式一:直接拼接
@RequestMapping(value="/register", method=RequestMethod.POST)
public String processRegistration( @RequestPart("profilePicture") MultipartFile profilePicture, @Valid Spitter spitter,
Errors errors,HttpServletRequest http) throws Exception{
profilePicture.transferTo(new File("/data/spittr/" + profilePicture.getOriginalFilename()));
if(errors.hasErrors()) {
return "registerForm";
}
spitterRepository.save(spitter);
return "redirect:/spitter/" + spitter.getUsername();
}
总结:利用连字符号实现,上述方式有点危险,在进行URL和SQL查询语句的时候
方式二:模型
@RequestMapping(value="/register", method=RequestMethod.POST)
public String processRegistration( @RequestPart("profilePicture") MultipartFile profilePicture, @Valid Spitter spitter,
Errors errors,Model model) throws Exception{
profilePicture.transferTo(new File("/data/spittr/" + profilePicture.getOriginalFilename()));
if(errors.hasErrors()) {
return "registerForm";
}
spitterRepository.save(spitter);
model.addAttribute("username", spitter.getUsername());
model.addAttribute("password",spitter.getPassword());
return "redirect:/spitter/{username}";
}
总结:
最后的效果为:/spitter/{username}?password="值"
示例如下:
这里采用是thymeleaf框架,获取重定向后的password的值
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <!-- 声明Thymeleaf命名空间 --><head><meta charset="UTF-8"/><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>Insert title here</title></head><body><h1>简介信息</h1><p th:text="${param.password[0]}" th:unless="${param.password == null}">Test</p></body></html>
填写注册信息:代码参考Spring异常处理章节
点击Register:效果
2、使用flash属性 利用它传递对象
首先修改一下控制类
@RequestMapping(value="/register", method=RequestMethod.POST)
public String processRegistration( @RequestPart("profilePicture") MultipartFile profilePicture, @Valid Spitter spitter,
Errors errors,RedirectAttributes model) throws Exception{
profilePicture.transferTo(new File("/data/spittr/" + profilePicture.getOriginalFilename()));
if(errors.hasErrors()) {
return "registerForm";
}
spitterRepository.save(spitter);
model.addAttribute("username", spitter.getUsername());
model.addFlashAttribute("spitter",spitter);
return "redirect:/spitter/{username}";
}
总结:
需要用到Spring提供RedirectAttributes的模型,其实Spring本质也是存入session,如果我们自己存的话需要我们管理,Spring它自会在用完之后销毁这个对象
再修改一下重定向的控制类,判断如果页面传过来就不要到数据库查询
@RequestMapping(value="/{username}", method=RequestMethod.GET)
public String showSpitterProfile(
@PathVariable String username, Model model){
if(!model.containsAttribute("spitter")){
Spitter spitter = spitterRepository.findByUsername(username);
model.addAttribute(spitter);
}
return "profile";
}
修改一下跳转的显示页profile.html
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <!-- 声明Thymeleaf命名空间 --><head><meta charset="UTF-8"/><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>Insert title here</title></head><body><h1>简介信息</h1>password:<span th:text="${spitter.password}" th:unless="${spitter.password == null}">Test</span><br/>username:<span th:text="${spitter.username}" th:unless="${spitter.username == null}">Test</span><br/>firstName:<span th:text="${spitter.firstName}" th:unless="${spitter.firstName == null}">Test</span><br/>lastName:<span th:text="${spitter.lastName}" th:unless="${spitter.lastName == null}">Test</span><br/>email: <span th:text="${spitter.email}" th:unless="${spitter.email == null}">Test</span><br/></body></html>
总结:如果是模型里的对象,可以直接通过添加的属性key进行对象的访问。
示例演示:
跳转页面:
- 第7章 Spring MVC的高级技术---跨重定向请求传递数据
- spring mvc 重定向后还能传递对象数据的方法
- Spring跨重定向请求传递数据跟踪实现
- spring mvc 请求重定向
- 第7章 Spring MVC的高级技术--异常处理
- 第7章-Spring MVC的高级技术
- 【spring mvc】 页面重定向时参数的传递
- spring mvc中重定向与请求转发的实现
- spring mvc中重定向与请求转发的实现
- spring中 mvc 的重定向与请求转发
- spring mvc 重定向时参数传递
- Spring mvc重定向传递参数
- Spring MVC重定向参数传递
- spring mvc 请求转发和重定向
- spring mvc 请求转发和重定向
- spring mvc 请求转发和重定向
- spring mvc 请求转发和重定向
- spring mvc 重定向和请求转发
- caffe 均值文件binaryproto 转mat
- SVM分类的一个具体例子
- POJ 2104 K-th Number(主席树入门)
- 面试必备手写解析Json串
- 有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个
- 第7章 Spring MVC的高级技术---跨重定向请求传递数据
- JavaScript函数作用域
- Java23种设计模式详解
- js跨域操作json数据
- 展开样式答题写法
- Android WebView 实现缓存网页数据
- 微信小程序开发,提示“不在以下合法域名列表中,请参考文档”
- Determining What Serial Ports Are Available on a Windows Machine
- 基于微信公众平台的二维码签到系统-----(2)二维码的生成以及内网穿透ngrok代理服务器的使用