Java Struts2 prevents repeated form submission code

Source: Internet
Author: User

Configure the interceptor to control repeated submissions! In fact, the principle is very simple. It is to generate a sequence in the form and determine whether it is a new sequence. If it is a previous sequence, it turns out to be a repeated submitted content!

The code is as follows: Copy code

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "common" extends = "struts-default" namespace = "/common">
<Interceptors>
<Interceptor-stack name = "loginStack">
<Interceptor-ref name = "defaultStack"> </interceptor-ref>
<Interceptor-ref name = "token"> </interceptor-ref>
</Interceptor-stack>
</Interceptors>
<Default-interceptor-ref name = "loginStack"> </default-interceptor-ref>
<Action name = "loginAction _ *" class = "loginAction" method = "{1}">
<Result name = "success">/welcome. jsp </result>
<Result name = "invalid. token" type = "redirect">/index. jsp </result>
<Result name = "input" type = "redirect">/index. jsp </result>
</Action>
</Package>
</Struts>

Note:

If you do not reference:

The code is as follows: Copy code

<Interceptor-ref name = "defaultStack"> </interceptor-ref> you cannot receive parameters in the background.

<Result name = "invalid. token" type = "redirect">/index. jsp </result>
<Result name = "input" type = "redirect">/index. jsp </result>

You must configure these two responses! One is the processing of repeated submissions, and the other is the processing without form tags!

Add Struts tag reference in JSP

The code is as follows: Copy code

<% @ Taglib prefix = "s" uri = "/struts-tags" %> add a tag to the form:

<S: form action = "" name = "form1" method = "post">
<S: token> </s: token>
</S: form>

Two hidden fields are displayed on the page:

The code is as follows: Copy code

<Input type = "hidden" name = "struts. token. name" value = "struts. token"/>
<Input type = "hidden" name = "struts. token" value = "bxpnndg6bb11zxhpi4e1_cz5k7vnmhr"/>

As mentioned above, there may be some friends who do not understand it. I will give you an example below.


Struts. xml configuration file

The code is as follows: Copy code

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>
<! -- Default view topic -->
<Constant name = "struts. ui. theme" value = "simple"/>

<! -- Struts2 has two interceptions to prevent repeated form submission: token and tokenSession. The tokenSession inherits the token.
Generally, the tokenSession client is friendly. -->
<! -- If the request is submitted repeatedly, the error. jsp page is displayed. -->
<Package name = "person" namespace = "/test" extends = "struts-default">
<Action name = "token" class = "com. ljq. action. PersonAction">
<Interceptor-ref name = "defaultStack"/>
<Interceptor-ref name = "token"/>
<! -- If the request is submitted repeatedly, go to the error. jsp page -->
<Result name = "invalid. token">/WEB-INF/page/error. jsp </result>
<Result>/WEB-INF/page/message. jsp </result>
</Action>
<Action name = "tokenSession" class = "com. ljq. action. PersonAction">
<Interceptor-ref name = "defaultStack"/>
<Interceptor-ref name = "tokenSession"/>
<! -- If repeated submissions are made, the error. jsp page is not displayed. -->
<Result name = "invalid. token">/WEB-INF/page/error. jsp </result>
<Result>/WEB-INF/page/message. jsp </result>
</Action>
</Package>
</Struts>

PersonAction class

The code is as follows: Copy code


Package com. ljq. action;


Import java. util. ArrayList;
Import java. util. List;

Public class PersonAction {
   
Private String name;

@ SuppressWarnings ("unchecked ")
// Watch the console
// If the token takes effect, the name value will not be output on the console, but the following warning will be output: 20:45:32 com. opensymphony. xwork2.util. logging. commons. CommonsLogger
// Warn warning: Form token EDZ4S96RNDN5VD8B1CQTK6FTHIJUPC66 does not match the session token null.
Public String execute (){
List ls = new ArrayList ();
Ls. add (name );
For (int I = 0; I <ls. size (); I ++ ){
System. out. println (ls. get (I ));
        }
Return "success ";
    }
   
Public String getName (){
Return name;
    }

Public void setName (String name ){
This. name = name;
    }
}

Index. jsp form page

The code is as follows: Copy code


<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
   
<Title> prevent repeated submission of forms </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
</Head>
 
<Body>
<! -- Prevent repeated submission of forms. Remember to enter <s: token> </s: token> --> in the form.
<! -- Action = "token", action = "tokenSession" -->
<S: form action = "token" namespace = "/test" method = "post">
Name: <s: textfield name = "name"/> <s: token> </s: token>
<Input type = "submit" value = "Send"/>
</S: form>
</Body>
</Html>

Message. jsp return success page

The code is as follows: Copy code

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
   
<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
</Head>
 
<Body>
<S: property value = "name"/> <br/>
<% = New Date () %>
</Body>
</Html>

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.