首先必須要通過(guò)一個(gè)Action再轉(zhuǎn)向那個(gè)添加記錄的頁(yè)面,轉(zhuǎn)向函數(shù)如下.
public ActionForward tokenTest(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
saveToken(request);//把一個(gè)token ID保存到Session,并在且要轉(zhuǎn)到的頁(yè)面
//的<html:form>中添加一個(gè)<input type="hideen">的標(biāo)答.
return mapping.findForward("add");
}
一個(gè)輸出入頁(yè)面如容如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>tokentest.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<br>
<logic:present name="error">
<pre style="color:#ff2255"><bean:write name="error"/></pre>
</logic:present>
<center>
<html:form action="/insert.do" method="post">
<table border="0" cellspacing="0" >
<tr>
<td width="30%">用戶名</td>
<td width="70%"><html:text property="username"/></td>
</tr>
<tr>
<td>地址:</td>
<td><html:text property="address"/></td>
</tr>
<tr>
<td colspan="2"><html:submit value="提交"/></td>
</tr>
</table>
</html:form>
</center>
</body>
</html:html>
頁(yè)面的處理Action內(nèi)容如下:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TokenTestForm tokenTestForm = (TokenTestForm) form;
if(!isTokenValid(request)){ //重復(fù)提交
request.setAttribute("error","不能得復(fù)提交!!!");
//saveToken(request); 重新生成tokenid,
return mapping.findForward("return");
}else{
resetToken(request);
}
//執(zhí)行相關(guān)操作
System.out.println(tokenTestForm.getUsername()+"--"+tokenTestForm.getAddress());
return mapping.findForward("ok");
}
至此已完成,至于原理,就自己去查一些資料就完全明白了....
2.來(lái)源http://www2.cnblogs.com/snoopy/articles/54699.html
使用Struts的Token機(jī)制解決表單的重復(fù)提交
前幾天被這個(gè)問(wèn)題困擾了,在Google中搜“表單重復(fù)提交”,也搜到不少資料,但有的講的不是很清楚,所以走了些彎路,現(xiàn)在寫(xiě)下來(lái),不能算原創(chuàng)吧。
Struts的Token(令牌)機(jī)制能夠很好的解決表單重復(fù)提交的問(wèn)題,基本原理是:服務(wù)器端在處理到達(dá)的請(qǐng)求之前,會(huì)將請(qǐng)求中包含的令牌值與保存在當(dāng)前用戶會(huì)話中的令牌值進(jìn)行比較,看是否匹配。在處理完該請(qǐng)求后,且在答復(fù)發(fā)送給客戶端之前,將會(huì)產(chǎn)生一個(gè)新的令牌,該令牌除傳給客戶端以外,也會(huì)將用戶會(huì)話中保存的舊的令牌進(jìn)行替換。這樣如果用戶回退到剛才的提交頁(yè)面并再次提交的話,客戶端傳過(guò)來(lái)的令牌就和服務(wù)器端的令牌不一致,從而有效地防止了重復(fù)提交的發(fā)生。
這時(shí)其實(shí)也就是兩點(diǎn),第一:你需要在請(qǐng)求中有這個(gè)令牌值,請(qǐng)求中的令牌值如何保存,其實(shí)就和我們平時(shí)在頁(yè)面中保存一些信息是一樣的,通過(guò)隱藏字段來(lái)保存,保存的形式如: 〈input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="6aa35341f25184fd996c4c918255c3ae"〉,這個(gè)value是TokenProcessor類(lèi)中的generateToken()獲得的,是根據(jù)當(dāng)前用戶的session id和當(dāng)前時(shí)間的long值來(lái)計(jì)算的。第二:在客戶端提交后,我們要根據(jù)判斷在請(qǐng)求中包含的值是否和服務(wù)器的令牌一致,因?yàn)榉?wù)器每次提交都會(huì)生成新的Token,所以,如果是重復(fù)提交,客戶端的Token值和服務(wù)器端的Token值就會(huì)不一致。下面就以在數(shù)據(jù)庫(kù)中插入一條數(shù)據(jù)來(lái)說(shuō)明如何防止重復(fù)提交。
在Action中的add方法中,我們需要將Token值明確的要求保存在頁(yè)面中,只需增加一條語(yǔ)句:saveToken(request);,如下所示:
public ActionForward add(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse
response)
//前面的處理省略
saveToken(request);
return
mapping.findForward("add");
}在Action的insert方法中,我們根據(jù)表單中的Token值與服務(wù)器端的Token值比較,如下所示:
public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
if
(isTokenValid(request, true)) {
// 表單不是重復(fù)提交
//這里是保存數(shù)據(jù)的代碼
} else
{
//表單重復(fù)提交
saveToken(request);
//其它的處理代碼
}
}
說(shuō)明:在前一個(gè)轉(zhuǎn)向提交信息的頁(yè)面需要saveToken(request);
在保存頁(yè)面使用
if(!isTokenValid(request)) { //重復(fù)提交 System.out.println("重復(fù)提交"); System.out.println("能得復(fù)提交!!!"); } else { request.setAttribute("saveInfo", info); logger.debug("save successful"); resetToken(request); //刪除session中的令牌 ward=mapping.findForward("notice"); }