Javatpoint Logo
Javatpoint Logo

Struts 2 Validation by Bundled Validators

Struts 2 validation framework provides many built-in validators also known as bundled validators for email, string, int, double, url, date etc

So, we don't need to provide explicit logic for email, double, url etc. For providing specific validation logic, we can use regex, which we will see later.

validation interceptor

It performs validation against the specified validation rules and adds the field-level and action-level error messages.

It works in conjunction with the workflow interceptor to display the error messages.

There is no parameter defined for this interceptor.


Advantage of Bundled validators

fast development because we don't need to specify the common validators such as email date, string length etc.


Bundled validators

Struts 2 provides following bundled validators.


Two ways to use bundled validators

There are two ways to use bundled validators:

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

In the next page we will see the full example of bundled validators. Let's now understand the difference between plain-validator syntax and field-validator syntax.


Plain-Validator (non-field validator) Syntax

Plain-validator syntax can be used for action level validator. In such case, a single validator can be applied to many fields.

But disadvantage of this approach is we can't apply many validators to single field.

Let's see simple example of plain-validator.


Field-Validator Syntax

The field-validator syntax can be used for field level validator. In such case, multiple validator can be applied to one field. For example, we can apply required and email validators on email field. Moreover, each field can display different messages.

But disadvantage of this approach is we can't apply common validator to many field like plain-validator.

Let's see simple example of field-validator.


Which validator syntax is better?

It is recommended to use field validator by apache struts because it provides more flexibility.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA