struts2防止重複提交的辦法,struts2防止提交

來源:互聯網
上載者:User

struts2防止重複提交的辦法,struts2防止提交

<?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>  <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <constant name="struts.devMode" value="true" />     <package name="default" namespace="" extends="struts-default">        <!-- 這裡是攔截器的配置 -->        <interceptors>            <interceptor name="test" class="com.test.interceptor.TestInterceptor"></interceptor>              <!-- 用於僅攔截某些方法 -->            <interceptor name="function" class="com.test.interceptor.FunctionInterceptor">                <param name="includeMethods">add</param>            </interceptor>               <interceptor-stack name="myStack">                 <interceptor-ref name="test"/>                 <interceptor-ref name="function"/>                 <interceptor-ref name="token"/>                 <interceptor-ref name="defaultStack"/>             </interceptor-stack>        </interceptors>            <!-- 正確和錯誤的分別跳轉,input是在驗證失敗後會跳轉的頁面 -->    <action name="data" class="com.test.action.ValideAction">        <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用攔截器的動作 -->         <result name="success">/result.jsp</result>        <result name="input">/login.jsp</result>        <result name="invalid.token">/token_error.jsp</result><!-- 防止重複提交的辦法 -->    </action>        <action name="FunctionInterceptor" class="com.test.action.ValideAction" method="add">        <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用攔截器的動作 -->         <result name="success">/result.jsp</result>        <result name="input">/login.jsp</result>         <result name="invalid.token">/token_error.jsp</result>    </action>    <action name="dele" class="com.test.action.ValideAction" method="dele">        <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用攔截器的動作 -->         <result name="success">/result.jsp</result>        <result name="input">/login.jsp</result>         <result name="invalid.token">/token_error.jsp</result>    </action>    </package></struts>

上面是struts.xml中的代碼,主要的代碼是:

 <result name="invalid.token">/token_error.jsp</result>

這樣一段。


<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><!-- 這個地方是用來顯示錯誤資訊 --><s:fielderror/><form action="data.action" method="post">使用者名稱:<input type="text" name="username"><br>密碼:<input type="password" name="password"><br>-------------------連絡方式----------------<br>手機:<input type="text" name="contact.phone"><br>地址:<input type="text" name="contact.address"><br>郵箱:<input type="text" name="contact.email"><br><s:token></s:token><!-- 防止重複提交,要加上這一段話 --><input type="submit" name="ok"><br><br><a href="FunctionInterceptor.action">測試FunctionInterceptor的add</a><br><br><a href="dele.action">測試FunctionInterceptor的dele</a><br><br></form></body></html>

在JSP頁面中,要加上這樣一段話,<s:token></s:token><!-- 防止重複提交,要加上這一段話 -->

就可以防止重複提交了。



struts2 怎防止重複提交

我是copy過來的= =

1、 通過重新導向的方式:把地址欄的地址顯示為最後的地址。
2、 禁止用戶端重新整理:通過JS來控制,禁用重新整理的功能、清除訪問曆史紀錄;
3、 Struts內建的方式(令牌):伺服器端發放一次性使用的令牌方式,伺服器端給用戶端每次請求發一個令牌,在用戶端提交之後,檢查令牌,如果這個令牌是第一次使用,就ok;否則,提示重複提交。
Action中有個幾個相關方法:
SaveToken(request):產生一個新的令牌,並儲存到用戶端的請求對象中。
IsTokenValid(request,true):檢查用戶端的令牌是否是第一次使用,並且使該令牌失效。
ResetToken(request):重設用戶端的令牌。
 
用struts2 token防止重新整理頁面重複提交

你好,下面的答案是從別人那裡複製過來,講的這樣,不知道是否對你有協助!!

在struts中,解決重複提交的問題,可以用Token令牌機制來處理。 原理: 伺服器端在處理用戶端的請求之前,會將請求中包含的令牌值與儲存在當前會話中的令牌值進行比較,看是否匹配。在處理完該請求後,並且在資訊達到用戶端之前,將產生一個新的令牌。該令牌值將會替換當前會話中的令牌值,並且傳到用戶端。這樣如果使用者回退到剛才的提交頁面並再一次提交的話,用戶端傳過來的令牌與服務其中的令牌值不一致,從而有效防止了提交。 實現: 首先在預添加的Action的execute()方法中建立並儲存一個令牌 saveToken(request); 功能:建立一個新令牌值,並且將它儲存到當前的session中,如果HttpSession對象不存在的話,就先建立這個對象。 由預添加的Action將令牌傳到了添加的頁面上,作為一個隱藏欄位。在添加頁面提交給添加AddAction後,在execute()方法中: 先判斷當前會話中的令牌值和請求中的令牌值是不是一致的: isTokenValid(request) 如果不是一致的,給出錯誤資訊,並且通過saveToken(request);重新整理權杖值。 如果是一致的,那麼就執行sql語句儲存(添加),再通過resetToken(request)方法刪除當前會話中的令牌。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.