处理在刷新情况下重复提交表单问题

在这里我是通过新加一个flag来判断它是否是重复提交,从而截断其多次提交。

   这个flag是在我要提交表单的这支jsp里面存入session里面的,同时在表单中使用<input type="hidden">把该flag值传入action,再在action中分别通过request.getParameter("flag")

和request.getSession.getAttribute("flag")来获取我的这个flag进行比较它们是否相等来判断是否提交。具体事例见如下:

1、首页看一个简单的preview.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="java.util.*"%>
<%
String validate_imageURL = request.getContextPath() + "/validate/validate_image.jsp";
response.setHeader("Cache-Control", "no-cache");
Random rand = new Random();
Integer flag = new Integer(rand.nextInt());
String str_flag = flag.toString();
session.setAttribute("sflag", str_flag);
%>
<s:form action="publicMessage" name="form1" method="post" theme="simple">
  <input type=hidden name="sflag" value="<%=str_flag%>" /> 
  <input type="text" name="name" value="">
  <input type="submit"  value='发表' class="reSubmit" >
</s:form>

2、 对应的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="message" extends="yepbook-public">
      
          <action name="publicMessage" class="lee.BugLogAction"   method="publicMessage">
            <result name="success" type="tiles">message.leave.layout</result>
            <result name="error" type="tiles">message.leave-error.layout</result>
        </action>

      </package>
</struts>

 

3、对应的class文件lee.PublicAction.java

package lee;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class BugLogAction extends ActionSupport implements ServletRequestAware
{
	public static final Log log = LogFactory.getLog(BugLogAction.class);
	private HttpServletRequest request;
                
                public String publicMessage()
	{
                     try{
                            Integer flag1 =  new Integer(Integer.parseInt(request.getParameter("sflag")));
	           String l_strflag1 = flag1.toString();
		if (l_strflag1.equals(request.getSession().getAttribute("sflag"))) 
		{
                                      String name=  request.getParameter("name");
                                 //其它要进行的操作 ...
                                        request.getSession().removeAttribute("sflag");
                                 }
                                  else
		{
		       request.getSession().removeAttribute("sflag");
		       log.warn("message save repeat");
		}
                         }catch (Exception e)
	        {
		e.printStackTrace();
		log.error(StringUtil.getStackTraceAsString(e));
		return ERROR;
	       }
	   return SUCCESS;
                 
                }

               public void setServletRequest(HttpServletRequest request)
	{
		this.request = request;
	}
}

 

你可能感兴趣的:(apache,jsp,xml,struts,cache)