STRUTS2 Study Notes (iii)

Source: Internet
Author: User

I. Validation of STRUTS2

1). Verification is divided into two types:

> Declarative Validation *

>> validation of which Action or Model field
>> What validation rules to use
>> If validation fails, turn to which page to display what error message

> Programmatic Validation

2). HelloWorld of declarative validation

I. Specify which field of the Action to validate: Age
Ii. Write the configuration file:
> Put struts-2.3.15.3\apps\struts2-blank\web-inf\classes\ Copy the Login-validation.xml file under Example to
under the package that contains the current Action.
> Change the configuration 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

& Lt;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, turn to the res of input Ult. So you need to configure Name=input 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>*

3). Note: 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. The validation rules for each action public can be
Configuration in it. However, it is important to note that the validation rules that apply only to a request for an action are not configured here.

4). 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"/>

5). 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--
<field-validator type= "Conversion" 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>
</field-validator>

6). If the type conversion fails, the following interceptors are also executed by default, and validation is performed. You can modify the Conversionerrorinterceptor source code in such a way that
When the type conversion fails, 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";
}
}

7). About Non-field validation: Not for validation of 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/>

8). The same validation rules are used for different fields, and the same response message is used?

Error.int=${gettext (FieldName)} needs to be between ${min} and ${max}

Age=\u5e74\u9f84
Count=\u6570\u91cf

For detailed analysis, see PPT 159.

9). 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 configuration file 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.

STRUTS2 Study Notes (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.