防止表单重复提交

=======表单使用<html:form/>

1,在进入表单的那个action代码里面加入 

this.saveToken(request);

这句话的意思是会在session里面生成一个唯一的一串数字:

org.apache.struts.action.TOKEN---26d6e6d73aa76c60c5ce09a8d2244417,如果使用的是<html:form>的话,就会自动生成一个影藏域,里面放着这串唯一的数字

2,当提交表单的时候,在需要提交的到的那个action里面:

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

if(!isTokenValid(request)){//判断是不是重复提交的,根据那串唯一的数字

System.out.println("我是重复提交");

return mapping.getInputForward();

}else {

System.out.println("我执行里面的代码");

resetToken(request);//要重设一下唯一的数字,以免下次重复提交

return mapping.findForward("success");

}

}

=======表单使用<form />

1,在表单的页面写上:

<c:set var="reflesh" value="hwt.page.refresh.true" scope="session"/>

将一个标志量放入放入到session的作用域中,然后提交之后到action里面取出来,看是不是这个值,如果是的就执行action里面需要执行的代码,然后改变里面这个session里面标志量的值,当再次刷新的时候,就不会在提交里面的代码了

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

String flag = (String) request.getSession().getAttribute("reflesh");

if(flag.equals("hwt.page.refresh.true")){

System.out.println("我执行里面的代码");

request.getSession().setAttribute("reflesh", "hwt.page.refresh.flase");

return mapping.findForward("success");

}else{

System.out.println("我是重复提交");

return mapping.getInputForward();

}

}

======也可以使用redirect来防止表单重复提交

不要让那个注册的action做两件事情,要做一个显示信息的action,然后重定向到那个action中,这样就可以防止页面的重复提交

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值