Struts2 prevents repeated instance code submission

Source: Internet
Author: User

First, let's talk about how repeated submissions are generated. Generally, there are two ways: 1. Refresh the page after page submission. 2. Click the submit button multiple times during submission. In strut1.x, the solution to prevent submission 1 is implemented through redirection, but method 2 can still submit data repeatedly when the network speed is slow or the user clicks the submit button quickly. In struts2, a solution is provided for method 2 (method 1 uses redirection to prevent repeated submissions caused by PAGE refreshing). struts2 uses tokens to solve such problems. To use token, first add <s: token/> to the form you want to submit on the page. After tag resolution, two hidden fields are generated: [html] <span style = "font-size: 12px;"> <input type = "hidden" name = "struts. token. name "value =" struts. token "/> <input type =" hidden "name =" struts. token "value =" LVYMI4CX9YBDS9A0AAF9UAJL8UDX1N05 "/> </span> A Random value (in red) is generated after the tag is executed. The value is added to the session. Next, add the token Interceptor to your struts. xml file. [Html] <package name = "token-struts" extends = "struts-default"> <interceptors> <interceptor name = "token"/> <interceptor-stack name = "token -default "> <interceptor-ref name =" token "/> <interceptor-ref name =" defaultStack "/> </interceptor-stack> </interceptors> <default-interceptor -ref name = "token-default"/> </package> If the annotation is used, the action should be defined as @ ParentPackage ("token-struts "). If it is not an annotation, you can add the interceptor stack above to your definition. This completes the entire task to prevent repeated submissions. If you want to know how this interceptor works, you can open the source code to see. Among them, TokenHelper. validToken () is the most critical. It is through this help class to judge the token. The above solves the problem of repeated submission, but it is a problem. As the name suggests, an interceptor is used to intercept requests. A request is intercepted before execution. By default, the interceptor intercepts all configured requests. However, in many cases, other requests under this configuration do not need to be intercepted, such as deletion or update operations. This interceptor is not required to intercept the task. The interceptor cannot perform different operations based on different requests. In this case, there are two solutions: first, TokenHelper is added to the action processing method you need to prevent repeated submissions. validToken (). If true is returned, it is executed. Otherwise, it is skipped. Second, use the token interceptor, but overwrite the TokenInterceptor. In the rewrite interceptor, tokenNames is determined. If the value is null, no operation is performed. If the value is null, interception is performed. 1. Use the form tag of Struts2, where the token tag must be added. Index. jsp form page [html] <% @ 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">

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.