Pages Navigation Menu

Coding is much easier than you think

Handling double form submissions in Struts2

Handling double form submissions in Struts2

 
Double form submissions(either by refreshing or by hitting the browser back button) can be irritating and in some cases leads to big problem .Luckly struts2 has a build in functionality that prevents such scenarios :<s:token/> tag and token interceptor. As an example, let’€™s say that we have a form that looks like this:
 

 
** UPDATE: Struts 2 Complete tutorial now available here.
 
In order to prevent double submissions all we have to do is to add tag in the above form. This tag will add a unique identifier in the form that will be used later from the token interceptor in order to check if the form is submitted for the first time or not. In case the form is not submitted for the first time, interceptor’s result will be “invalid.token”.
 




/inputPage.jsp
/invalidToken.jsp
/successPage.jsp

Note that “invalid.token” result in the above action declaration actually means that in the case of invalid token the user should be redirected in invalidToken.jsp
 
Recommended Article

 

5 Comments

  1. its block even first time request..
    please reply

  2. i tried adding the interception in my struts.xml and along with its result as invalid.token
    and then added the in my jsp page,
    when i try to access the form for the first time its not allowing me to enter into form instead its directly showing error
    whatever i have defined in the invalid_token.jsp

    plz me help me out

  3. This will work but after adding

    to struts.xml, Serverside field/action validation is not working..
    Kindly do me help on this.

    • Please mail your code without library to [email protected]

      • Dear Mohaideen this is vamana me also similar kind of issue even for first time hit also it is throwing error. Please help me.