0

Using Cancel button in struts

Cancel button is a type of submit button which bypasses validation during submission of the form. So, cancel button can be used for submitting a form without validation and to reset form.

  1. Using Cancel button for bypassing validation

    Reset button is also available in struts but reset button resets to the form fields to default if the form has not been submitted or has not be passed through validator. Once the validation has occured and validation failed, the form fields are populated with the values we have send for validation. Now by pressing reset button will retain the typed and not default values. In this case, struts cancel button is used. Struts cancel button can be rendered through
    <html:cancel/>.

    It is rendered in html as
    <input type="reset" value="Reset"><input type="submit"
    name="org.apache.struts.taglib.html.CANCEL" value="Cancel"
    onclick="bCancel=true;">

    The onclick="bCancel=true;" in generated reset button plays a great role for bypassing clientside validation.

    If you are using dispactch action, create a method called 'cancelled' and get the form field values inside that method.

    Cancel button bypassed both serverside and clientside validation.
  2. Using Cancel button as reset button
    If you're using Struts 1.2.9 or above, you must specify cancellable="true" in the Action mapping as;


    <action
    attribute="registrationForm"
    input="..."
    ...
    ...
    <set-property property="cancellable" value="true"/>
    ...
    ...
    </action>
    now its time to reset form fields through action like;

    if(isCancelled(request)){
    registrationForm.set("firstname",
    "");
    }
    [I'm using DynaValidatorForm here]

    If you are using DispatchAction then you have to create a method named cancelled and reset forms there.



|

0 Comments

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.