(九)Struts2 防重复提交
所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)
第一节:重复提交示例演示
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="manager" extends="struts-default"> 9 10 <action name="student" class="com.wishwzp.action.StudentAction" method="add"> 11 <result name="success">/success.jsp</result> 12 13 14 </action> 15 16 </package> 17 18 </struts>
Student.java
1 package com.wishwzp.model; 2 3 public class Student { 4 5 private String name; 6 private String age; 7 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getAge() { 15 return age; 16 } 17 public void setAge(String age) { 18 this.age = age; 19 } 20 21 @Override 22 public String toString() { 23 return "Student [name=" + name + ", age=" + age + "]"; 24 } 25 26 27 }
StudentAction.java
1 package com.wishwzp.action; 2 3 import com.wishwzp.model.Student; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class StudentAction extends ActionSupport{ 7 8 /** 9 * 10 */ 11 private static final long serialVersionUID = 1L; 12 13 private Student student; 14 15 public Student getStudent() { 16 return student; 17 } 18 19 public void setStudent(Student student) { 20 this.student = student; 21 } 22 23 public String add() throws Exception { 24 System.out.println("开始添加学生:"+student); 25 Thread.sleep(5000); 26 System.out.println(student.getName()+"添加完成"); 27 return SUCCESS; 28 } 29 30 }
student.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 12 <form action="student" method="post" > 13 14 姓名:<input type="text" name="student.name"/><br/> 15 年龄:<input type="text" name="student.age"/><br/> 16 <input type="submit" value="提交"/> 17 </form> 18 </body> 19 </html>
success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 学生添加成功! 12 </body> 13 </html>
结果显示:
如果我们反复的提交的话,后台就会反复提交很多次的数据,而这些数据都会提交到后台的,我们现在要避免这样的情况发生。
这样我们第二节就可以防这样的重复提交
第二节:使用<s:token/>标签防重复提交
<s:token></s:token> :加在form 里;
使用token 拦截器:
在struts.xml必须配置
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="invalid.token">/student.jsp</result> :在struts.xml 里配置,假如出现重复提交,则直接回到页面;
<s:actionerror/>:在页面上显示错误信息;
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 9 <package name="manager" extends="struts-default"> 10 11 <action name="student" class="com.wishwzp.action.StudentAction" method="add"> 12 <result name="success">/success.jsp</result> 13 <!-- 在struts.xml 里配置,假如出现重复提交,则直接回到页面 --> 14 <result name="invalid.token">/student.jsp</result> 15 16 <!-- 使用token 拦截器 --> 17 <interceptor-ref name="token"></interceptor-ref> 18 <interceptor-ref name="defaultStack"></interceptor-ref> 19 20 </action> 21 22 </package> 23 24 </struts>
Student.java
1 package com.wishwzp.model; 2 3 public class Student { 4 5 private String name; 6 private String age; 7 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getAge() { 15 return age; 16 } 17 public void setAge(String age) { 18 this.age = age; 19 } 20 21 @Override 22 public String toString() { 23 return "Student [name=" + name + ", age=" + age + "]"; 24 } 25 26 27 }
StudentAction.java
1 package com.wishwzp.action; 2 3 import com.wishwzp.model.Student; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class StudentAction extends ActionSupport{ 7 8 /** 9 * 10 */ 11 private static final long serialVersionUID = 1L; 12 13 private Student student; 14 15 public Student getStudent() { 16 return student; 17 } 18 19 public void setStudent(Student student) { 20 this.student = student; 21 } 22 23 public String add() throws Exception { 24 System.out.println("开始添加学生:"+student); 25 Thread.sleep(5000); 26 System.out.println(student.getName()+"添加完成"); 27 return SUCCESS; 28 } 29 30 }
student.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <s:actionerror/> 12 <form action="student" method="post" > 13 <s:token></s:token> 14 姓名:<input type="text" name="student.name"/><br/> 15 年龄:<input type="text" name="student.age"/><br/> 16 <input type="submit" value="提交"/> 17 </form> 18 </body> 19 </html>
success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 学生添加成功! 12 </body> 13 </html>
结果显示:
这样使用防重复提交拦截器,我们不管在前端页面按多少次提交,后天数据就一个数据,不会出现像上面很多次的数据了。
不过这样的我们可以看到这样的错误信号,我们可不可以直接无视这样的防重复提交呢!这里我们第三节就讲的这个。
第三节:使用tokenSession 拦截器防重复提交
tokenSesssion 拦截器直接无视重复提交的请求;
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
例子:
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 9 <package name="manager" extends="struts-default"> 10 11 <action name="student" class="com.wishwzp.action.StudentAction" method="add"> 12 <result name="success">/success.jsp</result> 13 14 <interceptor-ref name="tokenSession"></interceptor-ref> 15 <interceptor-ref name="defaultStack"></interceptor-ref> 16 17 </action> 18 19 </package> 20 21 </struts>
Student.java
1 package com.wishwzp.model; 2 3 public class Student { 4 5 private String name; 6 private String age; 7 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getAge() { 15 return age; 16 } 17 public void setAge(String age) { 18 this.age = age; 19 } 20 21 @Override 22 public String toString() { 23 return "Student [name=" + name + ", age=" + age + "]"; 24 } 25 26 27 }
StudentAction.java
1 package com.wishwzp.action; 2 3 import com.wishwzp.model.Student; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class StudentAction extends ActionSupport{ 7 8 /** 9 * 10 */ 11 private static final long serialVersionUID = 1L; 12 13 private Student student; 14 15 public Student getStudent() { 16 return student; 17 } 18 19 public void setStudent(Student student) { 20 this.student = student; 21 } 22 23 public String add() throws Exception { 24 System.out.println("开始添加学生:"+student); 25 Thread.sleep(5000); 26 System.out.println(student.getName()+"添加完成"); 27 return SUCCESS; 28 } 29 30 }
student.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <s:actionerror/> 12 <form action="student" method="post" > 13 <s:token></s:token> 14 姓名:<input type="text" name="student.name"/><br/> 15 年龄:<input type="text" name="student.age"/><br/> 16 <input type="submit" value="提交"/> 17 </form> 18 </body> 19 </html>
success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 学生添加成功! 12 </body> 13 </html>
结果显示:
这样的话我们不管在前端页面提交多少次,后台也只有一个数据,而且我们自己在前端页面也无法感受到,是不是很神奇呢!