首先必須要通過一個Action再轉向那個添加記錄的頁面,轉向函數如下.
public ActionForward tokenTest(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
saveToken(request);//把一個token ID保存到Session,并在且要轉到的頁面
//的<html:form>中添加一個<input type="hideen">的標答.
return mapping.findForward("add");
}
一個輸出入頁面如容如下:
<%@ 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>
頁面的處理Action內容如下:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TokenTestForm tokenTestForm = (TokenTestForm) form;
if(!isTokenValid(request)){ //重復提交
request.setAttribute("error","不能得復提交!!!");
//saveToken(request); 重新生成tokenid,
return mapping.findForward("return");
}else{
resetToken(request);
}
//執行相關操作
System.out.println(tokenTestForm.getUsername()+"--"+tokenTestForm.getAddress());
return mapping.findForward("ok");
}
至此已完成,至于原理,就自己去查一些資料就完全明白了....
2.來源http://www2.cnblogs.com/snoopy/articles/54699.html
使用Struts的Token機制解決表單的重復提交
前幾天被這個問題困擾了,在Google中搜“表單重復提交”,也搜到不少資料,但有的講的不是很清楚,所以走了些彎路,現在寫下來,不能算原創吧。
Struts的Token(令牌)機制能夠很好的解決表單重復提交的問題,基本原理是:服務器端在處理到達的請求之前,會將請求中包含的令牌值與保存在當前用戶會話中的令牌值進行比較,看是否匹配。在處理完該請求后,且在答復發送給客戶端之前,將會產生一個新的令牌,該令牌除傳給客戶端以外,也會將用戶會話中保存的舊的令牌進行替換。這樣如果用戶回退到剛才的提交頁面并再次提交的話,客戶端傳過來的令牌就和服務器端的令牌不一致,從而有效地防止了重復提交的發生。
這時其實也就是兩點,第一:你需要在請求中有這個令牌值,請求中的令牌值如何保存,其實就和我們平時在頁面中保存一些信息是一樣的,通過隱藏字段來保存,保存的形式如: 〈input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="6aa35341f25184fd996c4c918255c3ae"〉,這個value是TokenProcessor類中的generateToken()獲得的,是根據當前用戶的session id和當前時間的long值來計算的。第二:在客戶端提交后,我們要根據判斷在請求中包含的值是否和服務器的令牌一致,因為服務器每次提交都會生成新的Token,所以,如果是重復提交,客戶端的Token值和服務器端的Token值就會不一致。下面就以在數據庫中插入一條數據來說明如何防止重復提交。
在Action中的add方法中,我們需要將Token值明確的要求保存在頁面中,只需增加一條語句:saveToken(request);,如下所示:
public ActionForward add(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse
response)
//前面的處理省略
saveToken(request);
return
mapping.findForward("add");
}在Action的insert方法中,我們根據表單中的Token值與服務器端的Token值比較,如下所示:
public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
if
(isTokenValid(request, true)) {
// 表單不是重復提交
//這里是保存數據的代碼
} else
{
//表單重復提交
saveToken(request);
//其它的處理代碼
}
}
說明:在前一個轉向提交信息的頁面需要saveToken(request);
在保存頁面使用
if(!isTokenValid(request)) { //重復提交 System.out.println("重復提交"); System.out.println("能得復提交!!!"); } else { request.setAttribute("saveInfo", info); logger.debug("save successful"); resetToken(request); //刪除session中的令牌 ward=mapping.findForward("notice"); }