调用redirectAttributes.addFlashAttribute("msg",message)之后,重定向回页面comfirm页面,页面中的session里面没有msg属性
@GetMapping("/toTrade")
public String toTrade(Model model) throws ExecutionException, InterruptedException {
OrderConfirmVo confirmVo = orderService.confirmOrder();
model.addAttribute("orderConfirmData",confirmVo);
return "confirm";
}
@PostMapping("/submit/order")
public String submitOrder(OrderSubmitVo orderSubmitVo,Model model, RedirectAttributes redirectAttributes) {
try {
SubmitOrderResponseVo responseVo = orderService.submitOrder(orderSubmitVo);
if (responseVo.getCode()==0){
// 下单成功,跳转支付页面
model.addAttribute("submitOrderResp",responseVo);
return "pay";
}else {
// 下单失败,回到提交订单页
String msg = "下单失败:";
switch (responseVo.getCode()){
// 令牌失效
case 1: {
msg+="订单信息过期,请刷新后再次提交";
break;
}
// 验价失败
case 2: {
msg+="订单商品价格发生变化,请确认后再次提交";
break;
}
// 锁库存失败
case 3: {
msg+="库存锁定失败,商品库存不足";
break;
}
}
redirectAttributes.addFlashAttribute("msg",msg);
return "redirect:http://order.gulimall.com/toTrade%22;
}
}catch (Exception e){
if (e instanceof NoStockException){
String message = ((NoStockException) e).getMessage();
log.info("message: "+message);
// TODO session里面并没有msg
redirectAttributes.addFlashAttribute("msg",message);
}
return "redirect:http://order.gulimall.com/toTrade%22;
}
}
前端代码:
填写并核对订单信息
session中无msg
页面回显msg
你可以把session
里所有值取出来看看
代码如下:
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println("key:"+key + ",value:" + session.getAttribute(key));
}
说不定它就在session里面,只不过不是普通的形式存储