Struts String Length Field Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2019   |   Print Email
- XML: using type=”stringlength” attribute in <validator> or <field-validator> elements.
- Annotation: using @StringLengthFieldValidator annotation type before setter method of the field or action method (in case of plain-validator).
1. Struts String Length Field Validator XML
Usage:
- Field-validator syntax:
<field name="fieldName"> <field-validator type="stringlength"> <param name="param name">param value</param> <message>validation error message</message> </field-validator> </field>
- Plain-validator syntax:
<validator type="stringlength"> <param name="param name">param value</param> <message>validation error message</message> </validator>
Parameters:
Parameter name | Required | Description |
fieldName | Name of the field (required if using plain validator syntax). | |
minLength | No | An integer number specifies minimum length of the field value. |
maxLength | No | An integer number specifies maximum length of the field value. |
trim | No | Bolean value. If set to true, trim the field value before validating its length. Default is true. |
minLengthExpression | No | OGNL expression used to obtain the minimum length value. |
maxLengthExpression | No | OGNL expression used to obtain the maximum length value. |
trimExpression | No | OGNL expression used to obtain the trim flag. |
Struts String Length Field Validator XML Examples:
- Field-validator example:
<field name="password"> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">12</param> <message>Your password must have from ${minLength} to ${maxLength} characters</message> </field-validator> </field>
- Only checking the minimum length:
<field name="password"> <field-validator type="stringlength"> <param name="minLength">6</param> <message>Your password must have at least ${minLength} characters</message> </field-validator> </field>
- Checking for a fixed length:
<field name="password"> <field-validator type="stringlength"> <param name="minLength">12</param> <param name="maxLength">12</param> <message>Your password must contain exactly ${maxLength} characters</message> </field-validator> </field>
- Using OGNL expressions for minimum and maximum length:
<field name="password"> <field-validator type="stringlength"> <param name="minLengthExpression">${minLengthValue}</param> <param name="maxLengthExpression">${maxLengthValue}</param> <message>Your password must have from ${minLength} to ${maxLength} characters</message> </field-validator> </field>
In this case, we have to implement two methods getMinLengthValue() and getMaxLengthValue() in the action or JavaBean class as follows:public int getMinLengthValue() { return 6; } public int getMaxLengthValue() { return 12; }
- Plain-validator example:
<validator type="stringlength"> <param name="fieldName">password</param> <param name="minLength">6</param> <param name="maxLength">12</param> <message>Your password must have from ${minLength} to ${maxLength} characters</message> </validator>
2. Struts @StringLengthFieldValidator Annotation
@StringLengthFieldValidator(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). |
minLength | No | The minimum length to check the field value. | |
minLengthExpression | No | OGNL expression used to obtain the minimum length value. | |
maxLength | No | The maximum length to check the field value. | |
maxLengthExpression | No | OGNL expression used to obtain the maximum length value. | |
trim | No | true | Whether to evaluate trim the field value before validating. |
trimExpression | No | OGNL expression used to obtain the trim flag. |
Struts @StringLengthFieldValidator Annotation Examples:
- Basic field-validator:
@StringLengthFieldValidator( minLength = "6", maxLength = "12", message = "Your password must have from ${minLength} to ${maxLength} characters" ) public void setPassword(String password) { this.password = password; }
- Specifying i18n key for the message:
@StringLengthFieldValidator( minLength = "6", maxLength = "12", message = "Default message", key = "form.validation.password" ) public void setPassword(String password) { this.password = password; }
- Plain-validator (annotating the action method):
@StringLengthFieldValidator( type = ValidatorType.SIMPLE, fieldName = "password", minLength = "6", maxLength = "12", message = "Your password must have from ${minLength} to ${maxLength} characters" ) 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 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
Comments