Struts source code-html: Precautions for Cancel tag Application

Source: Internet
Author: User
The html: Cancel tag in Struts is a common tag used in Form. The main function is the current Form of cancel, which is generally written as follows:
  1. <Html: cancel>
  2. <Bean: message key = "createuser. cancelbutton"/>
  3. </Html: cancel>
This tag generates the following HTML code:
<Input type = "submit" name = "org.apache.struts.taglib.html. CANCEL" value = "return">
BCancel = true is a piece of javascript, and bCancel is a variable in the Javascript code that Struts automatically adds when Struts is used as Validator.
A brief summary of this section of Javascript is as follows:

  1. <Script type = "text/javascript" language = "Javascript1.1">
  2. <! -- Begin
  3. Var bCancel = false;
  4. Function validateCreateUserForm (form ){
  5. If (bcancel)
  6. Return true;
  7. Else
  8. Return validatemaxlength (form) & validaterequired (form) & validateminlength (form );
  9. }
  10. ... Omitted below
As you can see from the above, when bCancel = true, Javascript will automatically submit the form (return true), that is, if we are in the background Action code
If no specific code is written for this Cancel action, the effect produced by this Cancel tag is exactly the same as that produced by the submit button !! (Because of this button
Type is also equal to submit)
Pay attention to this point! Therefore, the following code is added to the execute method of the Action class to process the Cancel Action:

  1. // Was this transaction canceled?
  2. If (iscancelled (request )){
  3. Return (mapping. findforward ("createusersuccess "));
  4. }
With the above Code, the Cancel action will have the corresponding processing code and go to the relevant page.
The problem has been solved, but in the spirit of studying Struts source code, we still need to study the above Code
OK. Let's take a look at where the isCancelled method is defined and what the content is?
First, we found that this method is defined in the Action class. The Code is as follows:

  1. /**
  2. * <P> Returns <code> true </code> if the current form's cancel button was
  3. * Pressed. This method will check if the <code> globals. cancel_key </code>
  4. * Request attribute has been set, which normally occurs if the cancel
  5. * Button generated by <strong> CancelTag </strong> was pressed by the user
  6. * In the current request. If <code> true </code>, validation completed MED
  7. * By an <strong> ActionForm </strong>'s <code> validate () </code> method
  8. * Will have been skipped by the controller servlet. </p>
  9. *
  10. * @ Param request The servlet request we are processing
  11. * @ See org.apache.struts.taglib.html. CancelTag
  12. */
  13. Protected boolean isCancelled (HttpServletRequest request ){
  14. Return (request. getAttribute (Globals. CANCEL_KEY )! = Null );
  15. }
Oh, it turns out that the key value Globals. CANCEL_KEY in the request object is bound to an object. If yes, it means that after you press the Cancel button,
Struts binds an object to the request object and names it with this key value.
Where does Struts bind this object? Naturally, let's start from scratch.
Starting from the ActionServlet process method, after multiple method calls, the root cause is finally found. It was originally in RequestProcessor. java, and the code is as follows:
  1. /**
  2. * <P> process an <code> httpservletrequest </code> and create
  3. * Corresponding <code> httpservletresponse </code>. </P>
  4. *
  5. * @ Param request the Servlet request we are processing
  6. * @ Param response the servlet response we are creating
  7. *
  8. * @ Exception ioexception if an input/output error occurs
  9. * @ Exception servletexception if a processing exception occurs
  10. */
  11. Public void process (httpservletrequest request,
  12. Httpservletresponse response)
  13. Throws ioexception, servletexception {
  14. //... Code omitted
  15. // Process any ActionForm bean related to this request
  16. ActionForm form = processActionForm (request, response, mapping );
  17. // The answer is in the processPopulate method.
  18. ProcessPopulate (request, response, form, mapping );
  19. If (! ProcessValidate (request, response, form, mapping )){
  20. Return;
  21. }
  22. /**
  23. * Populate the properties of the specified ActionForm instance from
  24. * The request parameters has ded with this request. In addition,
  25. * Request attribute <code> Globals. CANCEL_KEY </code> will be set if
  26. * The request was submitted with a button created
  27. * <Code> canceltag </code>.
  28. *
  29. * @ Param request the Servlet request we are processing
  30. * @ Param response the servlet response we are creating
  31. * @ Param form the actionform instance we are populating
  32. * @ Param mapping the actionmapping we are using
  33. *
  34. * @ Exception servletexception if thrown by requestutils. populate ()
  35. */
  36. Protected void processpopulate (httpservletrequest request,
  37. Httpservletresponse response,
  38. Actionform form,
  39. Actionmapping mapping)
  40. Throws servletexception {
  41. If (form = NULL ){
  42. Return;
  43. }
  44. // Populate the bean properties of this actionform instance
  45. If (log. isdebugenabled ()){
  46. Log. debug ("populating bean properties from this request ");
  47. }
  48. Form. setservlet (this. servlet );
  49. Form. Reset (mapping, request );
  50. If (mapping. getmultipartclass ()! = NULL ){
  51. Request. setattribute (globals. multipart_key,
  52. Mapping. getmultipartclass ());
  53. }
  54. Requestutils. populate (Form, mapping. getprefix (), mapping. getsuffix (),
  55. Request );
  56. // Set the cancellation request attribute if appropriate
  57. If (request. getparameter (constants. cancel_property )! = NULL) |
  58. (Request. getParameter (Constants. CANCEL_PROPERTY_X )! = Null )){
  59. Request. setAttribute (Globals. CANCEL_KEY, Boolean. TRUE );
  60. }
  61. }

OK. Check the last few lines of code. Struts obtains the Constants. CANCEL_PROPERTY parameter from the request. If this parameter is not empty
TRUE: This object uses Globals. CANCEL_KEY as the key value and is stored in the request object.
As for what the value of Constants. CANCEL_PROPERTY is, you can now guess that it is the html code generated by the HTML: Cancel tag.
The name of the Cancel button! After checking, it is:

  1. <Input type = "submit" name = "org.apache.struts.taglib.html. CANCEL" value = "return">

Constants.cancel_propertyvalue: org.apache.struts.taglib.html. CANCEL

At this point, the truth is clear, it seems that you should be careful when using the html: Cancel label later :)

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.