Repeated submission of forms by users will have very serious consequences in some cases. For example, when using a credit card for online payment, if the server responds too slowly, the user may click the submit button multiple times, which may result in the amount of the credit card being consumed several times. Therefore, repeating the form will have a logical effect on your system, and some measures must be taken to prevent this from happening.
The reason that users repeatedly submit the same HTML form is to quickly click the Submit button repeatedly, and then press the browser's refresh button after submitting the form.
Set up Struts 2 prevention form repeat submit feature
Struts 2 has built-in functionality that prevents users from repeatedly submitting the same HTML form. How it works: Let the server generate a unique tag and keep a copy of the tag on the server and in the form. Thereafter, when the user submits the form, the markup in the form is sent to the server along with the other request parameters, and the server compares the token he received with the tag it retained. If the two matches, the submitted form is considered valid and the server will make the necessary processing and re-set a new tag. Subsequent submission of the same form will fail because the token on the server has been reset.
The token tag in the Struts 2 tab can be used to generate a unique tag. This tag must be nested within the form tag, which inserts a hidden field in the table dropdowns and saves the tag to the HttpSession object. The toke tag must be used in conjunction with the token or token session interceptor, and two interceptors can handle the token tag. When the token interceptor encounters a duplicate submission form, it returns a "Invalid.token" result and adds an action-level error. The token session blocker extends the token interceptor and provides a more complex service, which takes a different approach than the token interceptor, and it just blocks the subsequent commits so that the user does not commit as many times as if they were only submitted once.
Example: Using the token blocker to prevent duplicate submissions of forms
Configuring Struts.xml files, declaring actions
<?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=" Avoidpackage "extends=" Struts-default "> <action name=" Avoid "class=" struts2.action.AvoidAction "> <interceptor-ref Name= "token" ></interceptor-ref> <interceptor-ref name= "Defaultstack" ></interceptor-ref > <result name= "Invalid.token" >/error.jsp</result> <result name= "Input" >/input.jsp </result> <result name= "Success" >/output.jsp</result> </action> </ Package> </struts>
At this point, you need to add a token interceptor to the action in the Declaration of the action, because the token interceptor is not in the Defaultstack interceptor stack, note that the interceptor needs to be placed on the first bit of the interceptor stack, because the logic that determines whether the form is repeatedly submitted should be before the form is processed.
Create an Action class
public class Avoidaction extends Actionsupport { private static final long Serialversionuid = 2676453800249807631l;
private String username; Private Date birthday; Public String GetUserName () { return username; } public void Setusername (String username) { this.username = username; } Public Date Getbirthday () { return birthday; } public void Setbirthday (Date birthday) { this.birthday = birthday; } @Override public String Execute () { try { thread.sleep (4000); } catch (Interruptedexception e) { e.printstacktrace (); } return SUCCESS; } }
This action logic is handled as pending for 4 seconds, giving us the opportunity to click the Submit button multiple times to test the effect.
To create a page:
input.jsp
<s:form action= "Avoid" > <s:token> </s:token> <s:textfield name= "username" label= "Enter your name" ></s:textfield> <s:textfield name= "Birthday" label= "Enter your Birthday" ></S: textfield> <s:submit value= "Submit" > </s:submit> </s:form>
To use Struts 2 to prevent form recurrence, you need to use the token tag in the form tag, he generates a unique identifier, submits it to the server along with other parameters, and the server determines whether the form is a duplicate submission based on the identifier generated by the token tag. This feature is done by the token interceptor.
error.jsp
<body>
Do not duplicate submissions form!
</body>
When the form is repeated, the token interceptor returns a "Invalid.token" result, which turns the page to the page, prompting the user for an error message.
output.jsp
<body>
Your Name: <s:property value= "username"/>
<br/>
Your Birthday: <s:property value= "Birthday"/>
</body>
If you do not submit the form repeatedly, the correct page is displayed.
Test
In the browser, enter: http://localhost:8081/AvoidDuplicateSubmissions/input.jsp, get the following interface
Click the "Submit" button multiple times to see the effect
As you can see, the token blocker's settings take effect, and he prevents duplicate submissions of the form and gives error hints
This time we only click on the commit (Please re-enter the URL, or back to the input page after the refresh, this is because the token is submitted once after it has been modified, the non-refresh identifier is not possible with the server-persisted identifiers)
As you can see, the form has been processed correctly.
Another interceptor that handles the form of duplicate submissions is tokensession, with no difference between using this interceptor and using a token interceptor, just referencing the interceptor, and the other is exactly the same as the token interceptor
<?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=" Avoidpackage "extends=" Struts-default "> <action name=" Avoid "class=" struts2.action.AvoidAction "> <interceptor-ref Name= "Tokensession" ></interceptor-ref> <interceptor-ref name= "Defaultstack" ></ interceptor-ref> <result name= "Invalid.token" >/error.jsp</result> <result name= "Input" >/input.jsp</result> <result name= "Success" >/output.jsp</result> </action> </package> </struts>
Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with
How do I use struts 2 to prevent forms from repeating submissions?