=======表单使用<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中,这样就可以防止页面的重复提交