[Original]java Web Learning Note 73:struts2 Learning path--strut2 to prevent duplicate submission of forms

Source: Internet
Author: User

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1. Overview

1) Duplicate submission of form

-If the form page is refreshed, then submitting the form does not count repeated submissions

-If the form page is not refreshed:

-Multiple Click submit button

-Submitted successfully, press "fallback", then click "Submit Button".

-On the Controller response page in the form of a forwarding case, if it has been submitted successfully, then click "Refresh (F5)"

The drawbacks of repeating commits:

-Increases the burden on the server

-Increases the burden on the server

2.strut2 How to resolve duplicate submissions for a form:

1) Add S:token sub-tags in s:form

> Generate a hidden field

> Add a property value to the session

> The value of the hidden field is consistent with the session's property values

2) Use token or Tokensession interceptor

> Both interceptors are not in the default interceptor stack, so manual configuration

     > If you use the token interceptor, you need to configure a token.valid result

> If you use the Tokensession interceptor, you do not need to configure any other result

3) Token vs Tokensession

> is the problem of resolving form repeat submission

> Use token blocker to go to token.valid this result

> Using the Tokensession Interceptor will also respond to that target page, but will not execute the method of the target action. As if nothing had happened.

4) You can use the S:actionerror label to display an error message that duplicates the form, which can be overwritten in an internationalized resource file. The message can be found in the struts-messages.properties file

Struts.message.invalid.token = Custom Prompt content

token.jsp

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <%@ taglib Prefix="s"URI="/struts-tags" %>4 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">5 <HTML>6 <Head>7 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">8 <title>Insert Title here</title>9 </Head>Ten <Body> One      A     <S:formAction= "Testtoken" > -         <S:token></S:token> -         <S:textfieldname= "UserName"label= "UserName"></S:textfield> the          -         <S:submit></S:submit> -     </S:form> -  +  - </Body> + </HTML>

Struts.xml

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>< Packagename= "Default"namespace="/"extends= "Struts-default"><Actionname= "Testtoken"class= "Com.jason.upload.app.TokenAction">
<!--<interceptor-ref name= "tokensession" ></interceptor-ref>-

<interceptor-ref name= "token" ></interceptor-ref >

<Interceptor-refname= "Defaultstack"></Interceptor-ref> <result>/success.jsp</result>

<result name= "Invalid.token" >/token-error.jsp</result> </Action></ Package></Struts>

Tokenaction.java

1  PackageCom.jason.upload.app;2 3 ImportCom.opensymphony.xwork2.ActionSupport;4 5  Public classTokenactionextendsActionsupport {6 7     /**8 * @Fields: Serialversionuid9      */Ten     Private Static Final LongSerialversionuid = 1L; One  A     PrivateString UserName; -  -      PublicString GetUserName () { the         returnUserName; -     } -  -      Public voidsetusername (String userName) { +          This. UserName =UserName; -     } +  A @Override at      PublicString Execute ()throwsException { -          -Thread.Sleep (2000); - System.out.println (userName); -         returnSUCCESS; -     } in  -}

[Original]java Web Learning Note 73:struts2 Learning path--strut2 to prevent duplicate submission of forms

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.