Use Validator to do page verification in Struts

xiaoxiao2021-03-06  98

1. Join the Validator plugin in the struts-config.xml file, add this plug-in, your app is not using the Validator environment, however, you want to use it if you need other configurations. Such as: 2. Add the ApplicationResources configuration file in the struts-config.xml file. Such as: Add this file is to provide non-programming customization and multilingual support for error messages. If we use the Chinese platform operating system, you will first look for the ApplicationResource_zh_cn.properties file by default, then ApplicationResources_zh.properties, if the first two files are not found, the ApplicationResources.Properties file will be found. In order to display the error prompt information on the page, we also need to add the following to the end of the ApplicationResources.properties file: Errors.Required = {0} is required.ErrorS.minLength = {0} cannot be Less Than {1} character .rrors.maxlength = {0} cannot berough Than {2} characterid = {0} is invalid.rrors.byte = {0} must be an byte.errors.short = {0} Must be an SHORT .rrrs.integer = {0} Must be an integer.rrors.long = {0} must be an long.errors.float = {0} must be an float.errors.double = {0} Must be an double.erro .date = {0} is not a date.errors.range = {0} is not in the range {1} .creditcard = {0 }.creditcard = {0} is not a valid credit card number.rrors.email = {0} is an an invalid e-mail address. The above is only the error type of error types that Struts now supported. If you customize new types of error verification, you will need to add your own content. The {0} in the above is referring to the parameters submitted by the error.

For example: When you need the "user name" on the page, you can't empty (that is, the ErrorS.Required), this {0} represents "User Name", so if you don't fill in the username, you will throw the following error: User Name is required. (You can modify Chinese as needed) We may have noticed that since the error prompt information needs to be configured, how is the "User Name" system in the above example? That's right! Also modify this configuration file, the content is as follows: visitcust.ror.name.Required =
Username This When "User Name" is empty, the Struts background program defines the unreasonable or more. In addition, the above "VisitCust.Name.Required" is specified when Validation.xml configures authentication content. See the following description. Note: In general, your system only needs an ApplicationResources file, so members of the development group do not add their own resource files. You only need to use multiple ApplicationResources files when your project group is developed, but your Struts-config.xml file will have the same number. 3. Create your ActionForm and let it inherit the org.apache.struts.validator.validatorform class. 4. Create your Action implementation and associate with the ActionForm defined above. It should be noted here that it must be set to True when defining this action, and does not implement its validate method in your defined actionform - this means you will use the ValidatorForm's validate method, so you can guarantee you Error verification is normal. 5. Configure the validation.xml file. The basic content is as follows:

Requires the verification page Name Check content In the ApplicationResource file determine the longest limit length maxlength 20 Note: ARG0 and Arg1 here represent the parameters enclosed in the ApplicationResources file.

转载请注明原文地址:https://www.9cbs.com/read-102692.html

New Post(0)