我们在注册某个网站的时候,有的时候在网速响应比较慢的时候,可能会点击多次提交按钮,这样的话在第一次请求还没有处理完成的情况下,对于同一个请求我们请求了多次。
下面先看一个重复请求的例子:
1.新建工程:SecondLearnStruts2Chap09
2.新建一个类Student:
package com.test.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 + "]";
}
}
这里建立了一个Student类,并重写了这个类的toString方法。
3.新建一个action类:StudentAction
package com.test.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.test.model.Student;
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;
}
}
这里写了一个添加函数add,在这个函数里面使进程延时5秒钟响应,以此来演示由于网速慢导致的延时。
4.写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>
<struts>
<package name="manager" extends="struts-default">
<action name="student" class="com.test.action.StudentAction" method="add">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
运行程序,编辑好学生姓名和年龄后,连续两次点击提交按钮,控制台输出为:
可以看到这里对于相同的内容提交了两次,第一次请求还没有处理完成的情况下又请求了一次,这里对这两次请求都进行了处理。
其实很多时候是不能够对相同的内容重复提交的,既第一次请求还没有处理完成时是不能第二次请求相同的请求的。struts2解决重复提交的方式有两种。先看第一种:使用<s:token/>标签防重复请求
1.在提交的form表单中添加<s:token></s:token>标签
<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>
2.使用token 拦截器
<struts>
<package name="manager" extends="struts-default">
<action name="student" class="com.test.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>
当用户对于同样的数据重复提交后,struts2会返回invalid.token以及错误信息。对于错误信息使用<s:actionerror/>标签显示
3.最终修改后的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>
4.运行程序,对同样的内容连续两次请求:
这里显示出来错误信息。第一次请求会被处理,其他的请求则不会被处理。
第二种防止重复请求的方式是:使用tokenSession 拦截器防重复提交
1.修改配置文件struts.xml
<struts>
<package name="manager" extends="struts-default">
<action name="student" class="com.test.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.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>
这里要注意,<s:token></s:token>标签依然要放在form表单中。
运行程序,连续多次点击提交按钮: