The HTML: Cancel tag in Struts is a label that is often used in Form, the main function is Cancel's current Form, generally written as follows:
============================================================================================================================================================================================================= =====
html: Cancel>
============================================================================================================================================================================================================= =====
This tag will generate the following HTML code:
BCANCEL = true is a JavaScript, BCANCEL is a variable that Struts automatically adds a variable in JavaScript code for us.
This JavaScript is a short summary of this as follows:
============================================================================================================================================================================================================= =====
VAR BCANCEL = FALSE;
Function ValidateCreateUserform (form) {
IF (BCANCEL)
Return True;
Else
Return ValidateMaxlength (Form) && ValidateRequired (Form) && ValidateMinLENGTH (FORM);
}
. . . I omitted below
============================================================================================================================================================================================================= =====
As can be seen, this BCANCEL = True, JavaScript will automatically submit forms (Return True), that is, if we are in the background Action
If you write a specific code for this Cancel action, the effect of this Cancel tab is completely consistent with the action generated by the Submit button! ! (Because of this button
Type is also equal to SUBMIT
This requires very much attention! Therefore, in general, we will add the following piece of code in the Execute method of the Action class to handle this CANCEL action:
============================================================================================================================================================================================================= ===== // WAS THIS TRANSAACTION CANCELLED?
IF (ISCANCELED (REQUEST) {
Return (Mapping.FindForward ("CreateUsersuccess"));
}
============================================================================================================================================================================================================= =====
With the above code, the Cancel action has the corresponding processing code and goes to the relevant page.
The original thing has been resolved, but in the spirit of the Struts source code research, we also need to study the above code.
OK, let's take a look at what is already defined in this method, what is the content?
First find that this method is defined in the Action class, the code is as follows:
============================================================================================================================================================================================================= =====
/ **
*
returns True code> if The Current Form's Cancel Button Was * Pressed. This Method Will Check if The
globals.cancel_key code>
* Request Attribute Has Been Set, Which Normal Or Occurs If The Cancel
* Button generated by ca ZENCELTAG strong> WAS PRESSED by the user
* in the current request. if true code>, validation performed
* by an Actionform strong> 's validate () code> Method
* Will Have Been Skipped by The Controller Servlet. P>
*
* @Param Request The Servlet Request WE Are Processing
* @see org.apache.struts.taglib.html.canceltag
* /
Protected Boolean ISCANCENCELED (HTTPSERVLETREQUEST Request) {
Return (Request.getattribute (Globals.cancel_Key)! = null);
}
============================================================================================================================================================================================================= =====
Oh, it turned out to find the globals.cancel_key this key value in the Request object to bind an object. If so, then you will press the CANCEL button,
Struts will bind an object in the Request object and name it with this key value.
Where is the STRUTS binding this object? Very nature, let us find out from the beginning
From the ActionServlet's Process method, it has been found in multiple methods, and finally found the root source. It turned out to be in RequestProcessor.java, the code is as follows:
============================================================================================================================================================================================================= ===== / **
*
process an httpservletRequest code> and create the
* CORRESPONDING httpservletResponse code>. p>
*
* @Param Request The Servlet Request WE Are Processing
* @Param Response The Servlet Response We Are Creating
*
* @Exception ioException if an input / output error occurs
* @Exception servletexception if a processing exception Occurs
* /
Public Void Process (httpservletRequest Request,
Httpservletresponse response
THROWS IOException, servletexception {
//. . . Some code is omitted
// process Any Actionform Bean Related to this request
Actionform Form = ProcessActionform (Request, Response, Mapping);
// The answer is in this processpopulate method
ProcessPopulate (Request, Response, Form, Mapping);
IF (! ProcessValidate (Request, Response, Form, Mapping) {
Return;
}
/ **
* Populate the Properties of the specified actionform instance from
* The Request Parameters Included with this request. in adduion,
* Request Attribute Globals.cancel_key code> Will Be set if
* The Request Was Submitted with a Button Created By
* canceltag code>.
*
* @Param Request The Servlet Request WE Are Processing
* @Param Response The Servlet Response We Are Creating
* @Param Form The Actionform Instance We are populating * @Param mapping the an actionMapping we are for use
*
* @Exception servletexception if thrown by request /ls.populate ()
* /
Protected Void ProcessPopulate (httpservletRequest Request,
HTTPSERVLETRESPONSE RESPONSE,
Actionform Form,
ActionMApping mapping)
Throws servletexception {
IF (Form == Null) {
Return;
}
// populate the bean proties of this actionform instance
IF (log.Indebugeload ()) {
Log.debug ("Populating Bean Properties from this Request");
}
Form.setServlet (this.servlet);
Form.reset (mapping, request);
IF (mapping.getmultipartclass ()! = null) {
Request.setttribute (Globals.Multipart_Key,
mapping.getMultipartClass ());
}
Requestutils.Populate (form, mapping.getprefix (), mapping.getsuffix (),
REQUEST;
// set the Cancellation Request Attribute IF Appropriate
IF ((Request.GetParameter (constants.cancel_property)! = NULL) ||
(Request.GetParameter (constants.cancel_property_x)! = null) {
Request.setttribute (globals.cancel_key, boolean.true);
}
}
============================================================================================================================================================================================================= =====
OK, see the last few lines of code, Struts gets constants.cancel_property from the request, if this parameter is not empty, then he will
True This object is in the Request object with globals.cancel_key as the key.
As for this constants.cancel_property, what is the value of this value, now you can guess, obviously html: Cancel this tag generated HTML code, the name of the Cancel button! Check it out, it is:
The value of constants.cancel_property is org.apache.struts.taglib.html.cancel
At this point, the truth is white, it seems to use HTML: Cancel, this label is careful :)