I wonder how struts 2 validation is performed开发者_如何学运维 without specifying Validate = true in struts config xml. Can you anyone tell me the flow of Struts 2 validation using validation framework.
Validation happens through a combination of the "validation" and "workflow" interceptors. There is no "validate" setup in the Struts 2 config file, because it's unnecessary.
Struts core has the validation framework that assists the application to run the rules to perform validation before the action method is executed.
Actions class works as a domain data and it looks for the properties in its Action Mapping File and it searches the field validators in theFileName-Validation.xml and all validators work as per the field defined in validation.xml. If there is any mismatching of data, it picks the message from the validation.xml and displays it to user.
Sample Employee-validation.xml :
<validators>
<field name="name">
<field-validator type="required">
<message>
The name is required.
</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">29</param>
<param name="max">64</param>
<message>
Age must be in between 28 and 65
</message>
</field-validator>
</field>
</validators>
This is the sample validation file for Employee model and request will be validated for properties name and age. If name field left empty validation will gives the error message as "The name is required" above the name input box And if the age entered is outside the limit of 29-64 validation will show an error as "Age must be in between 28 and 65" above the age input box.
精彩评论