struts2 防止重复提交 实例代码
首先说说重复提交是怎么产生的,一般情况下有两种方式:
1,页面提交后再次刷新页面。
2,在提交的时候多次点击提交按钮。
strut1.x中解决防止提交1的方法是通过重定向解决,但是方式2在网速很慢或者是用户快速的点击提交按钮时,还是能够重复提交数据。
struts2中为方式2提供了解决方案(方式1用重定向是也可以防止用户刷新页面而引起的重复提交),struts2通过使用令牌(token)解决此类的问题。
要使用token,首先在页面上在你要提交的表单中加上<s:token/>,次标签解析后会生成两个隐藏域:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 您已经提交了表单,请不要重复提交。 </body></html>