表单重复提交

表单重复提交:
每次添加的时候会生成一个唯一的标志,判断标志是否存在,
存在,先删除标志,执行下一步操作,不存在,则证明已经
进行过一次操作,视为重复操作
1.在去添加页面的toAdd中随机生成一个token标志,
保证每次执行添加的时候都会有一个唯一的token值
2.将token标志放入redis缓存中
3.将token标志用隐藏标签放在add.jsp的表单中
4.控制台add接受token,判断token存在与否
存在:删除token,进行一次提交
不存在:证明已经提交,发生重复提交
三大作用域:
https://blog.youkuaiyun.com/weixin_39528000/article/details/114279792

@RequestMapping("toAdd")
public String toAdd(HttpServletRequest request){
    //随机生成一个token标志,放入缓存,防止重复提交
    String token = UUID.randomUUID().toString();
    redisTemplate.opsForValue().set(token,"123");
    request.setAttribute("token",token);
    //获取所有求职意向
    List<Intention> intentionList = notesService.getIntentions();
    request.setAttribute("intentionList",intentionList);
    //获取性别枚举
    request.setAttribute("genders", Gender.values());
    return "add";
}
<%--
  Created by IntelliJ IDEA.
  User: Cy
  Date: 2022/3/25
  Time: 20:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../../resource/bootstrap4/css/bootstrap.css">
    <script src="../../resource/js/jquery-3.2.1.js"></script>
    <script src="../../resource/bootstrap4/js/bootstrap.js"></script>
    <script src="../../resource/bootstrap-treeview/js/bootstrap-treeview.js"></script>
    <title>添加页面</title>
</head>
<body>
<form>
    <input type="hidden" name="token" value="${token}">
    <input name="notesName" placeholder="请输入简历名称"><br><br>
    <input name="name" placeholder="请输入姓名"><br><br>
    求职意向:
    <input name="intentions" type="hidden" id="intentions">
    <c:forEach items="${intentionList}" var="i">
        <input type="checkbox" name="yid" class="yid" value="${i.intentionId}">${i.intentionName}&nbsp;&nbsp;&nbsp;&nbsp;
    </c:forEach><br><br>
    出生日期:<input type="date" name="birthDate"><br><br>
    <input name="record" placeholder="请输入学历"><br><br>
    选择性别:
    <c:forEach items="${genders}" var="g">
        <input type="radio" name="sex" value="${g}">${g.text}
    </c:forEach><br><br>
    <input name="account" placeholder="填写个人简介"><br><br>
    <input type="button" value="保存" onclick="save()">
</form>
</body>
<script>
    function save() {
        //获取选中的课程信息
        let yids = "";
        $(".yid:checked").each(function () {
            yids+=$(this).val()+",";
        })
        //为隐藏域赋值
        $("#intentions").val(yids);
        $.post("add",$('form').serialize(),function (msg) {
            if (msg=="ok"){
                alert("添加成功")
            }
            if (msg=="repeatCommit"){
                alert("重复提交")
            }
        })
    }
</script>
</html>

@RequestMapping("add")
@ResponseBody
public String add(Notes notes,String token){
    //若token不存在,发生重复提交
    if (!redisTemplate.hasKey(token)){
        return "repeatCommit";
    }else {
        //若token存在,删除缓存中token,进行一次提交
        redisTemplate.delete(token);
        notesService.add(notes);
    }
    return "ok";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大风~刮过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值