Struts2.1 annotation防止表单重复提交
来源:互联网 发布:手机农村淘宝 编辑:程序博客网 时间:2024/05/01 20:11
问题描述:用户提交表单时可能因为网速的原因,或者网页被恶意刷新,致使同一条记录重复插入到数据库中,这是一个比较棘手的问题。我们可以从客户端和服务器端一起着手,设法避免同一表单的重复提交。 1.使用客户端脚本 上述方法,主要是将按钮的提交状态改变为disabled。 2.利用Struts2的令牌机制。 在页面加载时,<s: token />产生一个GUID(Globally Unique Identifier,全局唯一标识符)值的隐藏输入框如: /WEB-INF/content/error.jsp /WEB-INF/content/test-ok.jsp TestAction.javaStruts2.1 annotation防止表单重复提交
解决方案:
提到客户端脚本,经常使用的是JavaScript进行常规输入验证。在下面的例子中,我们使用它处理表单的重复提交问题,请看下面的代码:
"multipart/form-data">
<input name="text" type="text" id="text" />
<input name="cont" value="提交" type="button" onClick="document.
register.cont.value='正在提交,请等待';document.register.cont.
disabled=true;document.the_form.submit();">
</form>
防止表单重复提交主要用的到标签是<s: token />,拦截器 <interceptor-ref name="token" />,还有一个默认的返回值<result name="invalid.token">/input.jsp</result>
<input type="hidden" name="struts.token.name" value="struts.token"/>
<input type="hidden" name="struts.token" value="BXPNNDG6BB11ZXHPI4E106CZ5K7VNMHR"/>
同时,将GUID放到会话(session)中;在执行action之前,“token”拦截器将会话token与请求token比较,如果两者相同,则将会话中的token删除并往下执行,否则向actionErrors加入错误信息。如此一来,如果用户通过某种手段提交了两次相同的请求,两个token就会不同。
下面用零配置来演示 token的作用
/WEB-INF/content/test-success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
</head>
<body>
<s:actionerror/>
<s:form action="test!save.action" method="POST">
<s:textfield name="message" label="请输入您的信息"/>
<s:token name="token"/>
<s:submit value="确定" />
</s:form>
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<s:actionerror/>
不能重复提交表单!
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
SAVE OK!
</body>
</html>
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Results({
@Result(name="invalid.token",location="/index.html",type="redirect")})
@InterceptorRefs({@InterceptorRef(value="token",params={"includeMethods","save"}),@InterceptorRef("defaultStack") })
public class TestAction extends ActionSupport {
private String message;
public String execute(){
return SUCCESS;
}
public String save(){
return "ok";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
- Struts2.1 annotation防止表单重复提交
- Struts2.1 annotation防止表单重复提交
- Struts2.1 annotation防止表单重复提交
- Struts2.1 annotation防止表单重复提交
- Struts2.1 annotation防止表单重复提交
- Struts2.1 annotation防止表单重复提交
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- struts2防止表单重复提交
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- Struts2防止表单重复提交 .
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- struts2 防止表单重复提交
- dede关键词维护自动加内链锚文本
- 文件上传类
- 分布式数据库教案
- gentoo镜像服务器切换
- 分页类
- Struts2.1 annotation防止表单重复提交
- (hdu1010)Tempter of the Bone
- dedecms如何给栏目文章加统计功能
- php的路径
- Android的Activity和Tasks详解
- TCP:传输控制协议
- 一个很乱很长的jfreechart例子
- 亲爱的vs,我太感谢你了。。。
- GridView导出Excel碰到的问题