一、重复提交的例子:
模拟一种情况,存在延时啊,系统比较繁忙啊啥的。
模拟延迟5s钟,用户点了一次提交,又点了一次提交,例子中模拟这种情况;
这样会造成重复提交;
com.cy.action.StudentAction.java:
package com.cy.action; import java.io.File; import org.apache.commons.io.FileUtils; import com.cy.model.Student; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport{ private static final long serialVersionUID = 1L; private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String add() throws Exception { System.out.println("开始添加学生:"+student); Thread.sleep(5000); System.out.println(student.getName()+"添加完成"); return SUCCESS; } }
struts.xml:
<struts> <package name="manage" extends="struts-default"> <action name="student" class="com.cy.action.StudentAction" method="add"> <result name="success">/success.jsp</result> </action> </package> </struts>
student.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="student" method="post" > 姓名:<input type="text" name="student.name"/><br/> 年龄:<input type="text" name="student.age"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
测试:
二、使用<s:token/>标签防重复提交
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="manage" extends="struts-default"> <action name="student" class="com.cy.action.StudentAction" method="add"> <result name="success">/success.jsp</result> <result name="invalid.token">/student.jsp</result> <interceptor-ref name="token"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
student.jsp修改为如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 重复提交的错误信息显示在这里 --> <s:actionerror/> <form action="student" method="post" > <!-- s:token的存在,让每次提交都有唯一的一个token令牌 到了struts.xml中的<interceptor-ref name="token">token拦截器,就会识别 比如说第一次提交的时候,肯定有一个标识记录在session里面,假如用户手贱又点了一次的话, form中token的值还是一样的,<interceptor-ref name="token">发现又来了这个玩意,肯定是重复提交。 会返回一个<result name="invalid.token">,将错误信息返回到student.jsp里面。 --> <s:token></s:token> 姓名:<input type="text" name="student.name"/><br/> 年龄:<input type="text" name="student.age"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
测试:
填写姓名和年龄,点击两次提交,发现界面提示信息:
而后台正常执行,只添加一条:
上面有个不好的地方就是:将重复提交的错误信息,返回到界面了。
但是有这种需求:将重复提交的忽略;无视重复提交的请求;
三、使用tokenSession 拦截器防重复提交 -- 这种方式挺好的。
com.cy.action.StudentAction.java:
package com.cy.action; import com.cy.model.Student; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport{ private static final long serialVersionUID = 1L; private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String add() throws Exception { System.out.println("开始添加学生:"+student); Thread.sleep(5000); System.out.println(student.getName()+"添加完成"); return SUCCESS; } }
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="manage" extends="struts-default"> <action name="student" class="com.cy.action.StudentAction" method="add"> <result name="success">/success.jsp</result> <!-- <result name="invalid.token">/student.jsp</result> <interceptor-ref name="token"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> --> <interceptor-ref name="tokenSession"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
student.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="student" method="post" > <s:token></s:token> 姓名:<input type="text" name="student.name"/><br/> 年龄:<input type="text" name="student.age"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
测试:
前台显示添加成功:
查看后台: