1.??? javascript ,设置一个变量,只允许提交一次。
<script?language="javascript">?
????var?checkSubmitFlg?=?false;?
?????function?checkSubmit()?{?
???????????????if?(checkSubmitFlg?==?true)?{?
???????????????????return?false;?
????????????????}
???????????????checkSubmitFlg?=?true;?
???????????????return?true;?
??????????}
??????document.ondblclick?=?function?docondblclick()?{?
????????????????window.event.returnValue?=?false;?
??????????}
??????document.onclick?=?function?doconclick()?{?
????????????????if?(checkSubmitFlg)?{?
????????????????????window.event.returnValue?=?false;?
????????????????}?
??????????}?
</script>?
????var?checkSubmitFlg?=?false;?
?????function?checkSubmit()?{?
???????????????if?(checkSubmitFlg?==?true)?{?
???????????????????return?false;?
????????????????}
???????????????checkSubmitFlg?=?true;?
???????????????return?true;?
??????????}
??????document.ondblclick?=?function?docondblclick()?{?
????????????????window.event.returnValue?=?false;?
??????????}
??????document.onclick?=?function?doconclick()?{?
????????????????if?(checkSubmitFlg)?{?
????????????????????window.event.returnValue?=?false;?
????????????????}?
??????????}?
</script>?
?
<html:form?action="myAction.do"?method="post"?onsubmit="return?checkSubmit();">
2.?? 还是javascript,将提交按钮或者image置为disable
<html:form?action="myAction.do"?method="post"?onsubmit="getElById('submitInput').disabled?=?true;?return?true;">? ?
<html:image?styleId="submitInput"?src="images/ok_b.gif"?border="0"?/> ?
</html:form>
<html:image?styleId="submitInput"?src="images/ok_b.gif"?border="0"?/> ?
</html:form>
3.??? 利用struts的同步令牌机制
?? 利用同步令牌(Token)机制来解决Web应用中重复提交的问题,Struts也给出了一个参考实现。
?? 基本原理:
?? 服务器端在处理到达的请求之前,会将请求中包含的令牌值与保存在当前用户会话中的令牌值进行比较,看是否匹配。在处理完该请求后,且在答复发送给客户端之前,将会产生一个新的令牌,该令牌除传给客户端以外,也会将用户会话中保存的旧的令牌进行替换。这样如果用户回退到刚才的提交页面并再次提交的话,客户端传过来的令牌就和服务器端的令牌不一致,从而有效地防止了重复提交的发生。
if?(isTokenValid(request,?true))?{?
//?your?code?here?
return?mapping.findForward("success");?
}?else?{?
saveToken(request);?
return?mapping.findForward("submitagain");?
} ?
//?your?code?here?
return?mapping.findForward("success");?
}?else?{?
saveToken(request);?
return?mapping.findForward("submitagain");?
} ?
Struts根据用户会话ID和当前系统时间来生成一个唯一(对于每个会话)令牌的,具体实现可以参考TokenProcessor类中的generateToken()方法。
1. //验证事务控制令牌,<html:form >会自动根据session中标识生成一个隐含input代表令牌,防止两次提交
2. 在action中:
//<input?type="hidden"?name="org.apache.struts.taglib.html.TOKEN"?
//?value="6aa35341f25184fd996c4c918255c3ae">?
?if?(!isTokenValid(request))?
?errors.add(ActionErrors.GLOBAL_ERROR,?new?ActionError("error.transaction.token"));?
resetToken(request);?//删除session中的令牌
//?value="6aa35341f25184fd996c4c918255c3ae">?
?if?(!isTokenValid(request))?
?errors.add(ActionErrors.GLOBAL_ERROR,?new?ActionError("error.transaction.token"));?
resetToken(request);?//删除session中的令牌
??? 3. action有这样的一个方法生成令牌
?protected?String?generateToken(HttpServletRequest?request)?{ ?
?HttpSession?session?=?request.getSession();?
?try?{?
???byte?id[]?=?session.getId().getBytes();?
???byte?now[]?=?
???new?Long(System.currentTimeMillis()).toString().getBytes();?
???MessageDigest?md?=?MessageDigest.getInstance("MD5");?
???md.update(id);?
???md.update(now);?
???return?(toHex(md.digest()));?
?}?catch?(IllegalStateException?e)?{?
???return?(null);?
?}?catch?(NoSuchAlgorithmException?e)?{?
?? ?return?(null);?
?}?
}?
?HttpSession?session?=?request.getSession();?
?try?{?
???byte?id[]?=?session.getId().getBytes();?
???byte?now[]?=?
???new?Long(System.currentTimeMillis()).toString().getBytes();?
???MessageDigest?md?=?MessageDigest.getInstance("MD5");?
???md.update(id);?
???md.update(now);?
???return?(toHex(md.digest()));?
?}?catch?(IllegalStateException?e)?{?
???return?(null);?
?}?catch?(NoSuchAlgorithmException?e)?{?
?? ?return?(null);?
?}?
}?