首先必须要通过一个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");
}