Struts Validator Development Guide
Make Struts to Validator .. 1
Validator-rules.xml file .... 2
Validator.xml file .... 2
Required Validator .. 3
Mask Validator .. 3
Range Validator .. 4
Maxlength Validator .. 4
MINLENGTH Validator .. 5
Email Validator .. 5
Writing of form bean .... 5
Struts-config.xml file configuration .... 5
Capture error message in JSP page .... 6
Capture the error message of the server ... 6
Capture the wrong information of JavaScript ... 6
Write extended struts validator check password input .... 7
1. Write a verifer (Validator) to implement a consistent check of two fields in Form: 7
2. Write a Validator-Rules file to add a custom checkor configuration: 8
3. Write a JSP page ... 9
4. Write the validator.xml configuration file: 10
Make struts to support Validator
To use the Validator feature in the Struts project, you first need to configure the Struts project to support the Validator framework. Struts allows the Struts project to support the Validator. This Plugin is provided by Struts, and users can also develop their own Validator. Plugin.struts The default Plugin is:
Org.apache.struts.validator.validatorPlugin.
The user must configure this PLUGIN in the struts-config.xml file to support the Struts project to support the Validator. The configuration method is relatively simple, open the struts-config.xml file through any text editor, add the following code at the end of this file, Restarting the J2EE server can be supported for Validator.
In the JBUILDER development environment, you can support the Struts project to support the Validator framework by the following method.
1. Right-click the Web project in the project manager (WebModule in our project).
2. Select the "Properties ..." menu item for the menu.
3. Select the "Web" node in the left tree structure of the pop-up dialog.
4. Check "Sturts in the" JSP / Servlet Frameworks "box on the right
1.1 "
The "Validation" child node under the node and the "struts" node.
5. Click the "OK" button to complete the configuration.
6. Check the struts-config.xml file, you can see the Validator Plugin in the configuration file.
Validator-rules.xml file
Validator-rules.xml file is the definition file for the verifier. Specifies the basic information of each checker and the JavaScript code that executes the verification. Struts provides fourteen verifiers by default, these verifiers can meet us The vast majority of input check requirements encountered in the usual project development. If these validers do not meet the user's verification requirements, then we can also develop our own verifier. Our project owner implements a check password Consistency Validator.
Struts provides verification comprises Required Validator, Requiredif Validator, MinLength Validator, MaxLength Validator, Mask Validator, Byte Validator, Short Validaotr, Integer Validator, Long Validator, Float Validator, Double Validator, Date Validator, Range Validator, IntRange Validator, FloatRange Validator, CrediteCard Validator, Email Validator and other verifiers. Behind I will tell some of the more commonly used verifiers .valiDator.xml file
Validator.xml file Use the verification rule for each of the JSP Forms that users need to be verified, this file is the file we need to configure in the Struts project development. The root node of this file is
One of the
Required Validator
This verifier is used to verify the Form's input data cannot be empty. If a Form's Field relies on this verifier, if the user does not enter the input data corresponding to this Field in the Form, the Required verifier will Trigger an Exception, this Exception error message can be captured on the page or the server.
example:
The above example illustrates that the verification rule of the TEST field cannot be empty, and the corresponding definition information in ApplicationResources.properties may be the following case:
Test.Testfield.DisplayName = Test field # The key = "test.testfield" defined in the
If the Validator checks that the user does not enter the Test this field, the information corresponding to the test.testfield is passed to the verifier as Test.Required parameters. If the error message is captured in the JSP page, the error message will be below. This form:
"The test field can't be empty, please re-enter"
Mask Validator
Mask Validator checks if the data entered by the user is in accordance with certain rules, and this rule is defined by the developer in the validator.xml file.
example:
field> The above Range Validator This verifier calibration field is within a number range. example: Range Validator requires three parameters (arg), the first parameter arg0 is the maximum and minimum of the name, the second and third parameters specified by the specified range, respectively, respectively, MAX and MIN. Maxlength Validator The MaxLength Verifier checks the maximum length of a string, it requires two parameters, the Arg0 description field name, the arg1 description maximum length. example: MINLENGTH VALIDATOR This verifier is similar to the MaxLength Validator, the parameter, the verification rule is the same, but the number of variables as the parameter is MINLENGTH. Email Validator This verifier checks the email field. When the email address entered by the user is not legal, the verifier throws an exception. example: Writing of Form Bean The Form Bean to be automatically verified using the Struts Validator Framework cannot inherit from ordinary actionform, must inherit from ValidatorForm or ValidatorActionform. Our project groups are inherited from ValidatorForm. ValidatorForm is the corresponding verifier corresponding to the current Form's call according to the Action of the Struts-Config.xml file, so the name attribute (name) of ValidatorActionForm uses the path property of the action in struts-config.xml, so the value of the Path property should match the name attribute of the FORM in Validator.xml. Struts-config.xml configuration configuration To use the Validator Framework, you must also work with the corresponding configuration in the struts-config.xml file. In addition to the configuration Validator Plugin, you need to configure the Action of the FORM you need to verify. Main There are two places that need to be configured, one is the The Validator's checking mechanism can define the server to check or verify the browser side via JavaScript. Both different ways have a big difference when writing in the JSP page. Capture the error message of the server The error message to capture the server check is relatively simple. You can display an error message in the page only in the JSP page, which can also customize the display style.
html: Messages>
It is shown to display the specific error message generated by the Validator framework.
Capture the wrong information of JavaScript
To capture the error message generated by JavaScript First, let's generate the JavaScript code generated by the JSP page and let the submit button trigger the corresponding check function when the user clicks the submission button. Implementation method is as follows:
First let JSP trigger the calibration function and use this HTML code:
Where Action specifies the action of this form, this is no different from ordinary Form, the onSubmit property specifies the verification function, its rules are: return space Validator ValidatorForm name (this)
Note that such a combination rule cannot be changed, otherwise the page error can not be verified. Please pay attention to the first letter of the ValidatorForm's name must be capitalized, regardless of whether it is capitalized in the struts-congfig.xml file. Here, you must use it as uppercase letters.
Then you have to generate a JavaScript code that you can implement in the JSP page. This step is also very simple, just add the following sentence at the end of the JSP page:
Where the FormName's name is consistent with the configuration in the struts-config.xml file. Samples of STATICJAVAScript True will use the JavaScript code to check (these JavaScript code is automatically generated by the Validator framework of Struts, we only You need to configure the validator.xml file), and StaticJavaScript When Validator will check with the server code, the check code of these serve doesn't need to be written. Written Struts Validator check password input
Using Struts's Validator check page Form is consistent, it is generally used in the check password. When the user enters two passwords, it is necessary to verify whether the user's two inputs are consistent. If it is inconsistent, it needs to be reported. Implement this function The steps are as follows:
1. Write a Validator to implement a consistent check of two fields in Form:
Package cn.com.syni.net800114.validator;
Import org.apache.commons.validator.validatorutil; import org.apache.commons.validator.field; import org.apache.commons.validator.Validator; Import Org.Apache.commons.validator.GenericValidator;
Import org.apache.struts.action.actionerro; import org.apache.struts.validator.resources;
Import javax.servlet.http.httpservletRequest;
public class ValidationUtil {public static boolean validateTwoFields (Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) {String value = ValidatorUtil.getValueAsString (bean, field.getProperty ()); String sProperty2 = field.getVarValue ( "secondProperty "); String value2 = validatorutil.getValueAsstring (bean, sproperty2);
IF (! genericvalidator.isblankornull (value) {try {if (! value.equals (value2)) {Errors.Add (Field.getKey (), Resources.getActionError (Request, Va, Field); Return False;} } Catch (Exception E) {Errors.Add (Field.getKey (), Resources.getActionError (Request, Va, Field)); Return False;}} Return true;}} 2. Write a Validator-Rules file, add a self Define the configuration of the checker:
Field.value; secondValue = secondfield.value;}}}}} {if (i == 0) {FOCUSFIELD = Field;} Fields [i ] = OTWOFIELDS [X] [1]; bvalid = false;} }}}}} {FOCUSFIELD.FOCUS (); Alert (Fields.Join ('/ n'));}}}}]> javascript> validator> 3. Write JSP page The Form is included in the field to be custom verified. Here is the two password type INPUT, the verifier will match the two fields entered by the user. If it is inconsistent, you will tell the user by pop-up dialog box: <% @ Taglib URI = "/ Web-INF / STRUTS-Bean.TLD" prefix = "bean"%> <% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%> <% @ Page ContentType = "Text / HTML; Charset = GBK"%> The final penultimate line 4. Write the validator.xml configuration file: Public ActionerRors Validate (ActionMapping Mapping, HttpServletRequest Request) {ActionerRors Errors = New ActionerRORS (); Return super.validate (mapping, request);