Struts Required String Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2019   |   Print Email
- 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; }
- Struts Form Handling Tutorial
- Struts Form Validation Tutorial
- Struts Date Range Field Validator Example
- Struts Integer Range Field Validator Example
- Struts String Length Field Validator Example
- Struts URL Validator Example
- Struts Required Field Validator Example
- Struts Field Expression Validator Example
Other Struts Tutorials:
- Introduction to Struts 2 framework
- Struts beginner tutorial (Eclipse + Tomcat + XML)
- Struts Beginner Tutorial with Annotations
- Struts beginner tutorial with Convention Plugin (zero-configuration)
- How to handle exceptions in Struts
- Send e-mail with attachments in Struts
- Struts File Upload Tutorial
- Struts - Spring - Hibernate Integration Tutorial
About the Author:
![](/images/NamAuthor.png)
Comments