防止表單重複提交服務端處理

來源:互聯網
上載者:User
本文章摘編、轉載需要註明來源 http://write.blog.csdn.net/postedit/8590787 

防止表單重複提交這個問題我們是必須處理的,很多時候我們都是在頁面做處理,但是這是不能完全杜絕這情況的出現,所以更好選擇是在服務端處理

我們先寫個類

package com.shadow.security.interceptor;import java.util.List;import java.util.Map;import org.springframework.util.Assert;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.shadow.extras.util.CommonManager;import com.shadow.struts.context.ContextManager;/** *  * @author shadow * @email 124010356@qq.com */@SuppressWarnings("serial")public class TokenInterceptor extends AbstractInterceptor {/** * token令牌標識 */public final static String TOKEN_NAME = "token";private List<String> beforeMethods;private List<String> afterMethods;@Overridepublic String intercept(ActionInvocation actionInvocation) throws Exception {Assert.notNull(beforeMethods);Assert.notNull(afterMethods);String method = actionInvocation.getProxy().getMethod();for (String before : beforeMethods) {if (before.startsWith(method)) {// 過濾方法遇到xx方法開頭的就產生出一個TOKEN令牌儲存到session中ContextManager.getSession().setAttribute(TOKEN_NAME,CommonManager.getUUID());return actionInvocation.invoke();}}for (String after : afterMethods) {if (after.startsWith(method)) {// 過濾方法遇到xx開頭的方法就擷取session的TOKEN令牌,比較頁面擷取過來的TOKEN值String token = (String) ContextManager.getSession().getAttribute(TOKEN_NAME);Map<String, Object> parameters = actionInvocation.getInvocationContext().getParameters();for (String key : parameters.keySet()) {if (key.equals(TOKEN_NAME)) {Object value = parameters.get(key);if (value instanceof String[]) {String token_value = ((String[]) value)[0];if (token_value.equals(token)) {ContextManager.getSession().setAttribute(TOKEN_NAME, null);return actionInvocation.invoke();}}}}return TOKEN_NAME;}}return actionInvocation.invoke();}public void setBeforeMethods(List<String> beforeMethods) {this.beforeMethods = beforeMethods;}public void setAfterMethods(List<String> afterMethods) {this.afterMethods = afterMethods;}}

然後配置xml

<bean id="custoken" class="com.shadow.security.interceptor.TokenInterceptor"><property name="beforeMethods"><list><value>toAdd</value><value>toEdit</value></list></property><property name="afterMethods"><list><value>add</value><value>edit</value></list></property></bean>

這樣就把這個類放到spring容器裡了,然後我們在struts的過濾鏈裡添加這個interceptor

<!-- 過濾鏈 --><interceptors><interceptor name="custoken" class="custoken" /><interceptor-stack name="baseStack"><interceptor-ref name="custoken" /><interceptor-ref name="chain" /><interceptor-ref name="i18n" /><interceptor-ref name="exception" /><interceptor-ref name="servletConfig" /><interceptor-ref name="prepare" /><interceptor-ref name="checkbox" /><interceptor-ref name="params" /><interceptor-ref name="conversionError" /></interceptor-stack></interceptors><!-- 重複提交處理 --><global-results><result name="token" type="redirect">/server/apply/error/999.html</result></global-results></package>

然後在頁面表單直接加入(各種頁面訪問session裡面的token自行修改即可)

<input type="hidden" value="${Session.token}" name="token" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.