第7章 Spring MVC的高级技术---跨重定向请求传递数据

概述:

大家都知道重定向和转发的区别,重定向其实两个请求,而转发是一次请求,在请求一次之后,该请求的数据会被销毁,现在问题是:

需要重定向的功能而且希望第一次请求的数据能复制到下一次请求中?如何实现呢?


有两种方式:

  1. 使用URL模板以路径变量和/或查询参数的形式传递数据(缺点就是有长度现在,传递数据类型单一)
  2. 通过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进行对象的访问。


示例演示:


跳转页面:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值