Overview
A robust web application must ensure that user input is legitimate and valid.
Input validation for STRUTS2
– Declarative validation based on the xwork Validation framework : STRUTS2 provides some built-in validators based on the Xwork Validation framework. The use of these validators does not require programming, as long as the validation procedure should be declared in an XML file. The content to be declared includes:
Validate the field of which Action or Model
What validation rules to use
What error messages should be sent to the browser side when validation fails
– Programmatic validation: Validating user input by writing code
HelloWorld for declarative validation
I. First specify which field of the Action to validate: Age
II. Write the configuration file:
> Copy the login-validation.xml file under Struts-2.3.15.3\apps\struts2-blank\web-inf\classes\example to the current Action Under the package.
> change the config file to: Change Login to the name of the current Action.
> Write validation rules: see the struts-2.3.15.3/docs/ww/docs/validation.html documentation.
> You can define error messages in the configuration file:
<field name= "Age" > <field-validator type= "int" > <param name= "min" >20</param> <param name= "Max" >50</param> <message> ^^ age needs to be between ${min} and ${max}</message > </field-validator> </field>
> The error message can be internationalized. Can <message key= "Error.int" ></MESSAGE>.
Add a key value pair to the internationalized resource file: error.int= ^^ ^age needs to be between ${min} and ${max}
III. If validation fails, then turn to the result of input. So we need to configure Name=input's result
<result name= "Input" >/validation.jsp</result>
Iv. How do I display an error message?
> If you are using non-simple, an error message is displayed automatically.
> If you are using a simple theme, you need to s:fielderror the tag or use the EL expression directly (using OGNL)
${fielderrors.age[0]} OR <s:fielderror fieldname= "age" ></s:fielderror>*
What if an action class can answer multiple action requests and multiple action requests use different validation rules?
> defines its corresponding validation file for each of the different action requests: Actionclassname-aliasname-validation.xml
> Non-aliased profiles: Validation rules in Actionclassname-validation.xml still work. You can configure the validation rules for each action public. However, it is important to note that only one The validation rules for the request of the action are not configured here again.
Struts2 built-in validation rules
Required: Ensure that the value of a given field is not a null value
Requiredstring: Ensure that the value of a given field is neither null nor blank.
The –trim parameter. The default is true, which means that struts rejects the front and back spaces before validating the field value.
Stringlength: Verify that a non-empty field value is not sufficient in length.
–minlength: The minimum length of the related field. If this parameter is not given, the field will not have a minimum length limit
–maxlength: The maximum length of the related field. If this parameter is not given, the field will not have a maximum length limit
–trim: Whether or not to remove the front and rear spaces before verifying
Date: Ensures that the value of a given date field falls within a given range
– Max: The maximum value of the related field. If this parameter is not given, the field will not have a maximum limit
–min: The minimum value of the related field. If this parameter is not given, the field will have no minimum limit
Email: Check if the given String value is a legitimate email
URL: Checks whether the given String value is a valid URL
Regex: Checks whether the value of a given field matches a given regular expression pattern.
–expresssion*: A regular expression to match
–casesensitive: Whether to distinguish the case of letters. Default is True
–trim: Whether to remove space before and after. Default is True
Int: Checks whether a given integer field value is within a range
–min: The minimum value of the related field. If this parameter is not given, the field will have no minimum limit
– Max: The maximum value of the related field. If this parameter is not given, the field will not have a maximum limit
Conversion: Checks whether the type conversion to the given Action property causes a conversion error. The validator can also add a custom message based on the default type conversion message
Expression and fieldexpression: used to verify whether a given field satisfies a OGNL expression.
– The former is a non-field validator, which is a field validator.
– The former generates an action error when validation fails, and the latter generates a field error when validation fails
–expression*: The OGNL expression used for validation
The principle of declarative validation framework:
> Struts2 a Validation interceptor is available in the default interceptor stack
> Each specific validation rule will correspond to a specific validator. There is a configuration file that associates the validation rule name with the validator. The validator is actually validated. The file is located under Com.opensymphony.xwork2.validator.validators default.xml
<validator name= "Required" class= "Com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
Short Circuit Verification:
If you use multiple validators for a field, all validations are performed by default. If you want the previous validator verification to not pass, the latter will no longer be verified, you can use a short-circuit verification
<!--set Short circuit verification: if the current validation does not pass, the following verification is no longer performed- short-circuit= "true"> <message>^conversion Error occurred</message> </field-validator> <field-validator type= "int" > <param Name= "min" >20</param> <param name= "Max" >60</param> <message key= "Error.int" > </message>
If the type conversion fails, the following interceptors are also executed by default, and validation is performed. You can modify the Conversionerrorinterceptor source code so that when the type conversion fails, the subsequent validation interceptors are no longer executed, and the result of input is returned directly
Object action = Invocation.getaction (); if (action instanceof validationaware) { Validationaware va = (validationaware) action; if (va.hasfielderrors () | | va.hasactionerrors ()) { return "input"; } }
About non-field validation: Not validation for a field.
<validator type= "expression" > <param name= "expression" ><![ Cdata[password==password2]]></param> <message>password isn't equals to Password2</message > </validator>
Display error messages for non-field validation, using the S:actionerror tag: <s:actionerror/>
The same validation rules are used for different fields, and the same response message is used?
Custom Validator:
I. Classes that define a validator
> Custom validators are required to implement Validator.
> can choose to inherit Validatorsupport or Fieldvalidatorsupport classes
> If you want to implement a generic validator, you can inherit the Validatorsupport
> If you want to implement a field validator, you can inherit Fieldvalidatorsupport
> Specific implementations can refer to existing validators.
> If the validator needs to accept an input parameter, you need to add a corresponding property to this parameter
Ii. Configuring the authenticator in a configuration file
> By default, Struts2 loads the Validators.xml file in the root directory of the classpath. Load the validator in the file. The file is defined in the same way as the default authenticator: Default.xml under Com.opensymphony.xwork2.validator.validators
> If there is no validator specified under the Classpath, load from the validator in Default.xml under Com.opensymphony.xwork2.validator.validators
III. Use: As with the current authenticator.
Iv. Sample code: Customizing a 18-bit ID card authenticator
Validation of Struts2