Struts2 prevents repeated submission of forms

Source: Internet
Author: User
Struts. xml configuration file
   
<? 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 submitted repeatedly, the error. jsp tutorial 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
   
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
   
<% @ 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
   
<% @ 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>
      
Error. jsp form repeated submission prompt page
   
<% @ Page language = "java" import = "java. util. *" pageencoding = "UTF-8" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<%
String path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>

<! Doctype html public "-// w3c // dtd html 4.01 transitional // en">
<Html>
<Head>
<Base href = "<% = basepath %>">

<Title> my jsp 'error. 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">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<! --
<Link rel = "stylesheet" type = "text/css tutorial" href = "styles.css">
-->

</Head>

<Body>
You have already submitted the form. Please do not submit it again.
</Body>
</Html>

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.