表单重复提交处理总结
表单重复提交处理总结
liuxw0226 发表于2年前
表单重复提交处理总结
  • 发表于 2年前
  • 阅读 151
  • 收藏 14
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: 程序设计中表单重复提交会造成数据的杂乱,总结一下避免方式

一、重复提交的处理方式

    1.在进行请求之后重定向到查询或者其他不操作数据库的请求
    2.将页面事件执行的按钮置灰,防止重复点击触发请求
    3.自定义设置token,在请求之前将随机的token值放入session中,执行请求时候讲session中的token值删除,重复提交时候因为session中的token值不存在,判断该请求已经提交过不处理
    4.数据库层加锁,对该记录的操作锁表,其余对该记录操作的请求要等待

二、struts2的token配置与项目实例

    Struts标签库里面有<s:token/>标签,在页面隐藏域中添加随机值得token参数,提交表单后一起作为参数传递到后台放入session,struts配置token后不允许相同tbken的表单提交。
期待结果:
    请求http://localhost:8080/token/index.action

    点击按钮:

    刷新页面:

    Struts2的token设置记录(maven、jetty):

 

    pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.dx</groupId>
  <artifactId>struts2token</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>struts2token Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

        <!-- junit -->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>

        <!-- struts2 -->
      <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-core</artifactId>
          <version>2.3.24.1</version>
      </dependency>
      <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-json-plugin</artifactId>
          <version>2.3.24.1</version>
      </dependency>

      <!-- jsp-api -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
      </dependency>

  </dependencies>
  <build>
    <finalName>struts2token</finalName>
    <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf8</encoding>
                </configuration>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.0.0.M3</version>
        </plugin>

    </plugins>


  </build>
</project>


    struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<package name="dx-default" extends="json-default" namespace="/">
	
    	<interceptors>
           <interceptor name="loginInterceptor" class="cn.dx.interceptor.LoginInterceptor"/>
           <interceptor-stack name="dxStack">
            	<interceptor-ref name="loginInterceptor" />
            	<!-- <interceptor-ref name="SysVisitStatusInterceprot" /> -->
              <interceptor-ref name="defaultStack"></interceptor-ref>
           </interceptor-stack>
     	</interceptors>
      
     	<default-interceptor-ref name="dxStack"/>

	</package>
 
 	<include file="struts/dx-stoken.xml" />


</struts>

    dx-token.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

        <package name="dx-token" extends="dx-default" namespace="/token">
                <action name="index" class="cn.dx.action.TokenAction" method="index">
                        <result name="index">index.jsp</result>
                </action>
                <action name="addTest" class="cn.dx.action.TokenAction" method="addTest">
                        <result name="success">success.jsp</result>
                        <result name="invalid.token">error.jsp</result>
                        <interceptor-ref name="defaultStack"/>
                        <interceptor-ref name="token"/>
                </action>
        </package>

</struts>

    web.xml配置:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>struts2token</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

       拦截器LoginInterceptor

package cn.dx.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by liuxiongwei on 2015/12/22.
 */
public class LoginInterceptor extends AbstractInterceptor{

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpServletResponse response= ServletActionContext.getResponse();
        HttpSession session=request.getSession();
        String reqURI = request.getRequestURI();
        System.out.println("reqURI = " + reqURI);
       /* if(reqURI.endsWith("index.action")){
            return actionInvocation.invoke();
        }else{
            if(session.getAttribute("currentUser")!=null){
                return  actionInvocation.invoke();
            }else{
                response.sendRedirect(request.getContextPath()+"/login.jsp");
            }
        }*/
        actionInvocation.invoke();
        return null;
    }
}

    action:TokenAction

package cn.dx.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by liuxiongwei on 2015/12/22.
 */
public class TokenAction extends ActionSupport{
    public String name;
    public String address;

    public String index() {
        System.out.println("进入首页成功!");
        return "index";
    }

    public String addTest(){
        System.out.println("token");
        System.out.println("name: "+name);
        System.out.println("address: "+address);
        return SUCCESS;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

    index.jsp

<%--
  Created by IntelliJ IDEA.
  User: liuxiongwei
  Date: 2015/12/22
  Time: 21:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
    <title>token测试</title>
</head>
<body>

    <s:form action="addTest" method="get">
        <s:token/>
        <s:submit type="button">测试token</s:submit>
    </s:form>

</body>
</html>

    success.jsp

<%--
  Created by IntelliJ IDEA.
  User: liuxiongwei
  Date: 2015/12/22
  Time: 22.58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>token第一次提交结果</title>
</head>
<body>
  <h1>token第一次提交结果!</h1>
</body>
</html>

    error.jsp

<%--
  Created by IntelliJ IDEA.
  User: liuxiongwei
  Date: 2015/12/22
  Time: 23:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>token重复提交</title>
</head>
<body>
  <h1 style="color: red">token重复提交!</h1>
</body>
</html>







  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
liuxw0226
粉丝 7
博文 19
码字总数 8419
×
liuxw0226
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: