3

I am using struts 2.2.3 in web application. I want to use struts2 annotations based validations for the form data submitted from UI. The scenario is: User fills all the form fields and click on submit button. I have created a Person class for storing registration form data and RegistrationAction which triggers the logic for registration. RegistrationAction holds reference to person object with respected getters/setters. Please suggest how to validate individual fields of Person class after form submission, using struts 2 annotations.

Thanks.

Arun
  • 141
  • 2
  • 4
  • 11

4 Answers4

1

Try to make it using the fieldName property of the Validations annotation. Something like the following:

@Validations( requiredFields = {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "person.name", message = "You must enter a value name.")} )

Please refer the below link for further reference:

Struts2 Validation-annotation

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • Actualy this annotation (@Validation()) is depricated since struts 2.1. Even mentioned in the link you provided. – Arun Jan 11 '12 at 09:14
  • 2
    yes i know about that :) alternate way to use JSR 303 validations and struts2 has already plugin for this [oval-plugin](http://struts.apache.org/2.2.3/docs/oval-plugin.html) – Umesh Awasthi Jan 11 '12 at 09:17
  • Actually my problem is not which annotations to use but how to use. I can still use struts 2 annotations as only one is deprecated. So as I have mentioned in my problem statement that where to write field level annotations in Action(holding reference to Person DTO) or PersonDTO carrying form data? – Arun Jan 11 '12 at 09:29
  • 3
    @Arun The [`@Validations` annotation](http://struts.apache.org/2.x/docs/validations-annotation.html) is not the same as the `@Validation` annotation. One is deprecated, `@Validations` is not. – Dave Newton Jan 11 '12 at 12:48
  • 1
    If I want to implement annotations based validation in struts 2. Is the oval-plugin is the best way? – Arun Jan 17 '12 at 13:21
1

On the setter method use

@RequiredFieldValidator(type=ValidatorType.FIELD, message="your message here")

@Validator of @Validations(...) not necessary

Brandon
  • 68,708
  • 30
  • 194
  • 223
0

You mention a separate Person class, so I am guessing that this is a model-driven Action or using a function like setPerson in the Action class. If you want to validate the individual fields within Person, you will need to use a VisitorFieldValidator in the relevant Action method, and then put field validations on the Person object itself.

For example in the Action

@VisitorFieldValidator(message = "")
public void setPerson(Person person) {
    this.person = person;
}

and in Person

@RequiredFieldValidator(message = "Last name is required.")
public void setLastName(String lastName) {
    this.lastName = lastName;
}
Erica Kane
  • 3,137
  • 26
  • 36
0

Have you tried the Struts2 Validation Annotation.Its quite simple and Straight forward all you need is to define the @Validation() annotation at Class level and Apply standard or custom annotations as per your use-case.

Refer official Annotation based Validation document.

Validation-Annotation

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
  • Actualy this annotation (@Validation()) is depricated since struts 2.1. Even mentioned in the link you provided. – Arun Jan 11 '12 at 09:14