1、重複提交的處理方式 html
1.在進行請求以後重定向到查詢或者其餘不操做數據庫的請求
2.將頁面事件執行的按鈕置灰,防止重複點擊觸發請求
3.自定義設置token,在請求以前將隨機的token值放入session中,執行請求時候講session中的token值刪除,重複提交時候由於session中的token值不存在,判斷該請求已經提交過不處理
4.數據庫層加鎖,對該記錄的操做鎖表,其他對該記錄操做的請求要等待 java
2、struts2的token配置與項目實例 web
Struts標籤庫裏面有<s:token/>標籤,在頁面隱藏域中添加隨機值得token參數,提交表單後一塊兒做爲參數傳遞到後臺放入session,struts配置token後不容許相同tbken的表單提交。
期待結果:
請求http://localhost:8080/token/index.action 數據庫
點擊按鈕: apache
刷新頁面: json
Struts2的token設置記錄(maven、jetty): api
pom.xml session
<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配置: app
<?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配置: jsp
<?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>