从提交页面到成功页面的跳转一般采用视图定位,由于视图定位是在服务端跳转的,如果用户在点击提交之后再次刷新页面,会导致重复提交,数据库的数据会有重复。
@RequestMapping("/toTrans")//
public String toTrans(ModelMap modelMap, HttpSession session
) {//!
//
//如果是转账,则先查询余额
String cardNo = (String) session.getAttribute("cardNo");
String balance = cardInfoService.findByCardNo(cardNo).getBalance();
modelMap.addAttribute("balance", balance);
String token = UUID.randomUUID().toString();
session.setAttribute("token",token);
modelMap.addAttribute("token",token);
//跳转到转账页面
return "trans";
}
```
<form id="transForm" class="am-form am-form-horizontal" action="/trans/doTrans.do" method="post">
<input name="bToken" id="bToken" value="${token}"><%--传参要用name、value,否则Controller找不到--%>
<div class="am-form-group">
<div class="am-u-sm-9 am-u-sm-push-3">
<button type="button" onclick="submitForm()" class="am-btn am-btn-primary">提交</button>
</div>
</div>
3、获取传参的令牌与session中的令牌比较,看是否一样,一样则进入转账成功页面,否则转账失败。注意进入转账成功页面后需要销毁令牌,以防重复提交。
@RequestMapping("/doTrans")//?
public String doTrans(ModelMap modelMap, @RequestParam String bToken, HttpSession session, @RequestParam String checkInCardNo, @RequestParam String realName, @RequestParam String money) {//!
try {
String cardNo = (String) session.getAttribute("cardNo");
String token = (String) session.getAttribute("token");
cardInfoService.forward(cardNo, checkInCardNo, money, realName);
if (!bToken.equals(token) || bToken == null || token == null) {
session.removeAttribute("token");/*此处可删可不删*/
return "fail";
}
session.removeAttribute("token");
return "success";//?为什么success.jsp放在web-inf下面,返回地址会是tans/web-inf...
} catch (Exception e) {
e.printStackTrace();
modelMap.addAttribute("msg", e.getMessage());
return "fail";
}
}
```
原文:https://www.cnblogs.com/HashMap-Fantasy/p/8972313.html