Struts Email Validator Example
- Details
- Written by Nam Ha Minh
- Last Updated on 01 August 2019   |   Print Email
The Struts validation framework provides the email validator to validate if value of a String field is a valid e-mail address. We can use this validator in two forms:
- XML: using type=”email” attribute in <validator> or <field-validator> elements.
- Annotation: using @EmailValidator annotation type to annotate setter method of the field or action method.
NOTES: This validator does not work if the field is empty, i.e. it does not treat the empty value as invalid e-mail. So it’s recommended to use this validator in conjunction with the required string validator.
1. Struts Email Validator XML
Usage:
- Field-validator syntax:
<field name="fieldName"> <field-validator type="email"> <message>validation error message</message> </field-validator> </field>
- Plain-validator syntax:
<validator type="email"> <param name="fieldName">myEmailFieldName</param> <message>validation error message</message> </validator>
Parameters:
Parameter name | Description |
fieldName | Name of the field to validate. Required if using plain validator syntax. |
Struts Email Validator XML Examples:
- Field-validator example:
<field name="myEmail"> <field-validator type="email"> <message>Please enter a valid e-mail address.</message> </field-validator> </field>
- Plain-validator example:
<validator type="email"> <param name="fieldName">myEmail</param> <message>Please enter a valid e-mail address.</message> </validator>
2. Struts EmailValidator Annotation
Usage:Annotate setter method of the String field or action method (in case of using plain-validator) by:
@EmailValidator(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). |
Struts Email Validator Annotation Examples:
- Basic field-validator:
@EmailValidator(message = "Please enter a valid email address") public void setMyEmail(String myEmail) { this.myEmail = myEmail; } - Specifying i18n key for the message:
@EmailValidator(message = "Default message", key = "form.validation.email") public void setMyEmail(String myEmail) { this.myEmail = myEmail; } - Plain-validator (annotating the action method):
@EmailValidator(type = ValidatorType.SIMPLE, message = "Please enter a valid email address 4", fieldName = "myEmail") public String execute() { return SUCCESS; }
Related Struts Form Validation Tutorials:
- Struts Form Handling Tutorial
- Struts Form Validation Tutorial
- Struts Date Range Field Validator Example
- Struts String Length Field Validator Example
- Struts Required Field Validator Example
- Struts URL 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:
Nam Ha Minh 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.
Comments