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:
- <Html: cancel>
- <Bean: message key = "createuser. cancelbutton"/>
- </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:
- <Script type = "text/javascript" language = "Javascript1.1">
- <! -- Begin
- Var bCancel = false;
- Function validateCreateUserForm (form ){
- If (bcancel)
- Return true;
- Else
- Return validatemaxlength (form) & validaterequired (form) & validateminlength (form );
- }
- ... 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:
- // Was this transaction canceled?
- If (iscancelled (request )){
- Return (mapping. findforward ("createusersuccess "));
- }
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:
- /**
- * <P> Returns <code> true </code> if the current form's cancel button was
- * Pressed. This method will check if the <code> globals. cancel_key </code>
- * Request attribute has been set, which normally occurs if the cancel
- * Button generated by <strong> CancelTag </strong> was pressed by the user
- * In the current request. If <code> true </code>, validation completed MED
- * By an <strong> ActionForm </strong>'s <code> 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 isCancelled (HttpServletRequest request ){
- Return (request. getAttribute (Globals. CANCEL_KEY )! = Null );
- }
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:
- /**
- * <P> process an <code> httpservletrequest </code> and create
- * Corresponding <code> 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 {
- //... Code omitted
- // Process any ActionForm bean related to this request
- ActionForm form = processActionForm (request, response, mapping );
- // The answer is in the 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 has ded with this request. In addition,
- * Request attribute <code> Globals. CANCEL_KEY </code> will be set if
- * The request was submitted with a button created
- * <Code> 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 actionmapping we are using
- *
- * @ Exception servletexception if thrown by requestutils. populate ()
- */
- Protected void processpopulate (httpservletrequest request,
- Httpservletresponse response,
- Actionform form,
- Actionmapping mapping)
- Throws servletexception {
- If (form = NULL ){
- Return;
- }
- // Populate the bean properties of this actionform instance
- If (log. isdebugenabled ()){
- Log. debug ("populating bean properties from this request ");
- }
- Form. setservlet (this. servlet );
- Form. Reset (mapping, request );
- If (mapping. getmultipartclass ()! = NULL ){
- Request. setattribute (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. setAttribute (Globals. CANCEL_KEY, Boolean. TRUE );
- }
- }
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:
- <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 :)