Validation Annotation Menu Toggle navigation Home Welcome Download Releases Announcements License Thanks! Sponsorship Privacy Policy Support User Mailing List Issue Tracker Reporting Security Issues Commercial Support End-of-Life Versions Version Notes Security Bulletins IntelliJ IDEA plugin Maven Project Info Struts Core Dependencies Plugin Dependencies Documentation Birds Eye Key Technologies Kickstart FAQ Wiki Getting Started Security Guide Core Developers Guide Tag Developers Guide Maven Archetypes Plugins Struts Core API Tag reference FAQs Plugin registry Contributing You at Struts How to Help FAQ Development Lists Submitting patches Source Code and Builds Coding standards Contributors Guide Release Guidelines PMC Charter Volunteers Source Repository Updating the website Edit on GitHub Validation Annotation This annotation has been deprecated since Struts 2.1 as its previous purpose, to define classes that support annotation validations, is no longer necessary. Usage Validation annotation must be applied at Type level. Parameters validations - list of validations to apply Examples An Annotated Interface Apply standard or custom annotations at method level public interface AnnotationDataAware { void setBarObj(Bar b); Bar getBarObj(); @RequiredFieldValidator(message = "You must enter a value for data.") @RequiredStringValidator(message = "You must enter a value for data.") void setData(String data); String getData(); } An Annotated Class public class SimpleAnnotationAction extends ActionSupport { @RequiredFieldValidator(type = ValidatorType.FIELD, message = "You must enter a value for bar.") @IntRangeFieldValidator(type = ValidatorType.FIELD, min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.") @StrutsParameter public void setBar(int bar) { this.bar = bar; } public int getBar() { return bar; } @Validations( requiredFields = {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "customfield", message = "You must enter a value for field.")}, requiredStrings = {@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "stringisrequired", message = "You must enter a value for string.")}, emails = { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "emailaddress", message = "You must enter a value for email.")}, urls = { @UrlValidator(type = ValidatorType.SIMPLE, fieldName = "hreflocation", message = "You must enter a value for email.")}, stringLengthFields = {@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="10" , maxLength = "12", fieldName = "needstringlength", message = "You must enter a stringlength.")}, intRangeFields = { @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "intfield", min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")}, dateRangeFields = {@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "datefield", min = "-1", max = "99", message = "bar must be between ${min} and ${max}, current value is ${bar}.")}, expressions = { @ExpressionValidator(expression = "foo > 1", message = "Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."), @ExpressionValidator(expression = "foo > 2", message = "Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."), @ExpressionValidator(expression = "foo > 3", message = "Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."), @ExpressionValidator(expression = "foo > 4", message = "Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."), @ExpressionValidator(expression = "foo > 5", message = "Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.") } ) public String execute() throws Exception { return SUCCESS; } } When multiple methods are used to map different actions on the same class, and one of them is annotated with @Validations, those validators will be triggered for all the actions, unless they are annotated with @SkipValidation or validateAnnotatedMethodOnly is set to true in the validation interceptor, like: <interceptor-ref name="validation"> <param name="validateAnnotatedMethodOnly">true</param> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> Copyright © 2000-2025 The Apache Software Foundation. Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are trademarks of The Apache Software Foundation. All Rights Reserved. Logo and website design donated by SoftwareMill. Follow @x