struts2學習(15)struts2防重復提交
阿新 • • 發佈:2017-07-02
div 模擬 sleep img exce version 提交 ftw har
一、重復提交的例子:
模擬一種情況,存在延時啊,系統比較繁忙啊啥的。 模擬延遲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; } publicString 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"> <actionView Codename="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>View Code
測試:
二、使用<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; } }View Code
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>
測試:
前臺顯示添加成功:
查看後臺:
struts2學習(15)struts2防重復提交