所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:重复提交示例演示

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="manager" extends="struts-default">

         <action name="student" class="com.wishwzp.action.StudentAction" method="add">
             <result name="success">/success.jsp</result>

         </action>

     </package>

 </struts>

Student.java

 package com.wishwzp.model;

 public class Student {

     private String name;
     private String age;

     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public String getAge() {
         return age;
     }
     public void setAge(String age) {
         this.age = age;
     }

     @Override
     public String toString() {
         return "Student [name=" + name + ", age=" + age + "]";
     }

 }

StudentAction.java

 package com.wishwzp.action;

 import com.wishwzp.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;
     }

 }

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>

success.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>
 学生添加成功!
 </body>
 </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

 <?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="manager" extends="struts-default">

         <action name="student" class="com.wishwzp.action.StudentAction" method="add">
             <result name="success">/success.jsp</result>
             <!-- 在struts.xml 里配置,假如出现重复提交,则直接回到页面 -->
             <result name="invalid.token">/student.jsp</result>

             <!-- 使用token 拦截器 -->
             <interceptor-ref name="token"></interceptor-ref>
             <interceptor-ref name="defaultStack"></interceptor-ref>

         </action>

     </package>

 </struts>

Student.java

 package com.wishwzp.model;

 public class Student {

     private String name;
     private String age;

     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public String getAge() {
         return age;
     }
     public void setAge(String age) {
         this.age = age;
     }

     @Override
     public String toString() {
         return "Student [name=" + name + ", age=" + age + "]";
     }

 }

StudentAction.java

 package com.wishwzp.action;

 import com.wishwzp.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;
     }

 }

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></s:token>
     姓名:<input type="text" name="student.name"/><br/>
     年龄:<input type="text" name="student.age"/><br/>
     <input type="submit" value="提交"/>
 </form>
 </body>
 </html>

success.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>
 学生添加成功!
 </body>
 </html>

结果显示:

这样使用防重复提交拦截器,我们不管在前端页面按多少次提交,后天数据就一个数据,不会出现像上面很多次的数据了。

不过这样的我们可以看到这样的错误信号,我们可不可以直接无视这样的防重复提交呢!这里我们第三节就讲的这个。

第三节:使用tokenSession 拦截器防重复提交

tokenSesssion 拦截器直接无视重复提交的请求;

<interceptor-ref name="tokenSession"></interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

例子:

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="manager" extends="struts-default">    

         <action name="student" class="com.wishwzp.action.StudentAction" method="add">
             <result name="success">/success.jsp</result>

             <interceptor-ref name="tokenSession"></interceptor-ref>
             <interceptor-ref name="defaultStack"></interceptor-ref>

         </action>

     </package>

 </struts>

Student.java

 package com.wishwzp.model;

 public class Student {

     private String name;
     private String age;

     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public String getAge() {
         return age;
     }
     public void setAge(String age) {
         this.age = age;
     }

     @Override
     public String toString() {
         return "Student [name=" + name + ", age=" + age + "]";
     }

 }

StudentAction.java

 package com.wishwzp.action;

 import com.wishwzp.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;
     }

 }

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></s:token>
     姓名:<input type="text" name="student.name"/><br/>
     年龄:<input type="text" name="student.age"/><br/>
     <input type="submit" value="提交"/>
 </form>
 </body>
 </html>

success.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>
 学生添加成功!
 </body>
 </html>

结果显示:

这样的话我们不管在前端页面提交多少次,后台也只有一个数据,而且我们自己在前端页面也无法感受到,是不是很神奇呢!