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:

    • XML: using type=”requiredstring” attribute in <validator> or <field-validator> elements.
    • Annotation: using @RequiredStringValidator annotation type to annotate setter method of the field.
 

1. Struts Required String Validator XML

Usage:

  • Field-validator syntax:
    <field name="fieldName">
    	<field-validator type="requiredstring">
    		<param name="param name">param value</param>
    		<message>validation error message</message>
    	</field-validator>
    </field>
     

  • Plain-validator syntax:
    <validator type="requiredstring">
    		<param name="param name">param value</param>
    		<message>validation error message</message>
    </validator> 
 

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:

  • Field-validator example:
    <field name="firstName">
    	<field-validator type="requiredstring">
    		<param name="trim">false</param>
    		<message>You must enter your first name</message>
    	</field-validator>
    </field>
     

  • Plain-validator example:
    <validator type="requiredstring">
    	<param name="fieldName">firstName</param>
    	<param name="trim">false</param>
    	<message>You must enter your first name</message>
    </validator> 
 

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:

  • Basic field-validator:
    @RequiredStringValidator(message = "Please enter your first name")
    public void setFirstName(String firstName) {
    	this.firstName = firstName;
    }
     

  • Specifying i18n key for the message:
    @RequiredStringValidator(message = "Default message", key = "form.validation.firstName")
    public void setFirstName(String firstName) {
    	this.firstName = firstName;
    }
     

  • Plain-validator (annotating the action method):
    @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "firstName",
    	message = "Please enter your first name")
    public String execute() {
    
    	return SUCCESS;
    } 
 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment