Struts 2 Validation by Bundled Validators

Struts 2 validation framework gives many inherent validators otherwise called packaged validators for email, string, int, twofold, url, date and so on

So, we don't have to give unequivocal rationale to email, twofold, url and so forth. For giving explicit validation rationale, we can utilize regex, which we will see later.

validation interceptor

It performs validation against the predefined validation guidelines and includes the field-level and activity level mistake messages.

It works related to the work process interceptor to show the blunder messages.

There is no parameter characterized for this interceptor.

Advantage of Bundled validators

fast development in light of the fact that we don't have to indicate the normal validators, for example, email date, string length etc.

Bundled validators

Struts 2 provides following bundled validators.

Two approaches to utilize packaged validators

There are two different ways to utilize packaged validators:

  1. Plain-Validator (non-field validator) Syntax
  2. Field-Validator Syntax

In the following page we will see the full case of packaged validators. We should now comprehend the contrast between plain-validator linguistic structure and field-validator syntax.

Plain-Validator (non-field validator) Syntax

Plain-validator grammar can be utilized for activity level validator. In such case, a solitary validator can be connected to numerous fields.

But disservice of this methodology is we can't have any significant bearing numerous validators to single field.

Let's see straightforward case of plain-validator.

  1. <validators>  
  2.     <!- -  Plain-Validator Syntax - - >  
  3.     <validator type="requiredstring">  
  4.         <param name="fieldName">username</param>  
  5.         <param name="trim">true</param>  
  6.         <message>username is required</message>  
  7.     </validator>  
  9. </validators>  

Field-Validator Syntax

The field-validator sentence structure can be utilized for field level validator. In such case, various validator can be connected to one field. For instance, we can apply required and email validators on email field. In addition, each field can show diverse messages.

But disservice of this methodology is we can't matter regular validator to many field like plain-validator.

Let's see straightforward case of field-validator.

  1. <validators>  
  2.     <!- -  Field-Validator Syntax - - >  
  3.     <field name="username">  
  4.           <field-validator type="requiredstring">  
  5.             <param name="trim">true</param>  
  6.             <message>username is required</message>  
  7.        </field-validator>  
  8.     </field>  
  10. </validators>  

Which validator punctuation is better?

It is prescribed to utilize field validator by apache swaggers since it gives more flexibility.

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition