The Struts validation framework provides the required string validator in order to check a String field is not null and not empty (“”). It comes with two flavors:

 

1. Struts Required String Validator XML

Usage:

 

Parameters:

Parameter name

Required

Default value

Description

fieldName

  

Required if using plain validator syntax

trim

No

true

A boolean value indicates whether to trim the field’s value before validating.

trimExpression

No

 

An OGNL expression used to trim the field’s value.

 

Struts Required String Validator XML Examples:

 

2. Struts @RequiredStringValidator Annotation

Usage: Annotate setter method of the String field by:

@RequiredStringValidator(param1 = "param 1 value", param2 = "param 2 value", ...)



 

Parameters:

Parameter name

Required

Default value

Description

message

Yes

 

validation error message.

key

No

 

i18n key for validation error message.

messageParams

No

 

Additional parameters to customize the message.

fieldName

No

 

Specifies field name in case this validator type is plain-validator.

shortCircuit

No

false

Whether this validator is short circuit.

type

No

ValidatorType.FIELD

type of the validator: field-validator (FIELD) or plain-validator (SIMPLE).

trim

No

true

Whether to trim the field’s value before validating.

 

Struts @RequiredStringValidator Annotation Examples:

 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.