仿struts1的防止表单重复提交  

package com.gc.struts.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 06-10-2010
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class YulanAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
   String token=this.generateToken(request);
      request.setAttribute("clientToken", token);
      request.getSession().setAttribute("token", token);
  return mapping.findForward("ok");//跳转到showExcel.jsp
 }
}

 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>showExcel.jsp Content</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>
  <%
     String clintToken=request.getAttribute("clientToken")==null?"":request.getAttribute("clientToken").toString();
   %>
    <form action="showExcel.do" method="post">
    <input type="hidden" value="<%=clintToken %>" name="clientToken"/>
    <input type="submit" value="提交" />
    </form>
  </body>
</html>

 

package com.gc.struts.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gc.struts.form.TestForm;

/**
 * MyEclipse Struts
 * Creation date: 06-10-2010
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class ShowExcelAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
     TestForm test=(TestForm)form;
     for(int i=0;i<1000000000;i++){
      
     }//这个是为了能多点几次提交
     String clientToken=test.getClientToken();
     Object sessionToken=request.getSession().getAttribute("token");
     System.out.println(sessionToken);
     if(sessionToken!=null&&!sessionToken.toString().equals(clientToken)){
      System.out.println("重复提交了");
      
     }else{
      System.out.println("提交表单");
     }
     String token=this.generateToken(request);
     request.getSession().setAttribute("token", token);
  return null;
 }
}

 

看看打印结果:

9694ec0baa72277bbeb071356b351cd4
提交表单
f55c7a7d71b2635130438a193bd30225
重复提交了
df7c73b27c0582613e7d11f79a15ae58
重复提交了
f2778990b6abbaaec84ba89e4de24dfc
重复提交了
80fb030346bfb023abb596a34eb05d5a
重复提交了
c071865f5efbe2e7aa127ac1d3ef83d3
重复提交了
9b68a90308a5ff4635c575f5a189c86f
重复提交了
62d557df874c638fbdd1172917e2b095
重复提交了
860d5ce47de84e953f69a70b13f6473f
重复提交了
8c310fedb5de55ce53a45e1a0d2aadb8
重复提交了
88b2a367f783475473928ac420d468f3
重复提交了

只提交了一次。

你可能感兴趣的:(struts1)