Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Wiki Markup
    get commons-validator.jar - go to \[http://jakarta.apache.org/ jakarta\] to download the latest version.
    \\
  • put it in your classpath - this is normally WEB-INF/lib/ along with the other jars such as struts.jar
  • create action forms to be validated - based on ValidatorForm, or just use DynaActionForms and don't worry about coding classes for them - all that's needed is the form config in struts-config.xml (see below)
  • edit your struts-config.xml:
    ** put the validator plug-in xml into the plug-ins section:
No Format
        <!-- sets up Validator -->
        <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
          <set-property 
            property="pathnames" 
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
        </plug-in>

...

  • to enable client-side validation: ** make sure you include the onsubmit attribute with the html:form tag
No Format
          <html:form action="/private/esurvey/survey/update" >
            focus="optionText"
            onsubmit="return validateForm(this);">

...

And finally, this is how struts actually deals with it all on submission of a form:1:

  1. It goes and checks for any instances of the form-bean in the scope (the name of the class, type, and scope is specified by the action mapping in struts-config.xml).

...

  1. If found then call the form's reset method

...

  1. If not found than a new instance is created and stored in the proper scope.

...

  1. Then the formBean is populated with request parameters by struts

...

  1. If the validate is true than the form's validate() method is called. There's no need to call it explicitly and because of ValidatorForm doing all the validation automatically, all that is required for code is a call in your form's submit() method to super.validate(). If you are using DynaValidatorForm, then you will not have program any form beans at all.

...

  1. if validate() returns any errors, then struts forwards the request to the URL specified in the input attribute of the action mapping.

...

  1. otherwise it checks for the action which the action mapping is based on.

Then after that you have a populated form. Normally you would then transfer the data into a value or data-transfer object to pass to your business layer (see BeanUtils.copyProperties).