Struts Input Validation with Annotation Based Validators |Video upload date:  · Duration: PT9M21S  · Language: EN

Learn Struts input validation with annotation based validators for cleaner action classes and easy rule maintenance with examples and testing tips.

If your forms are a legal minefield and your users keep discovering new ways to type garbage into fields then annotation based validation in Struts can be your seat belt and airbag at once. Keep rules next to the action code so reviewers do not need a treasure hunt to find why an email was required or why a name had to be 2 characters minimum.

Why use annotation based validators

Annotations make validation visible during code review and reduce the chance that someone forgets a check in the template. They are great for small to medium form sets and for teams that like their rules close to the business logic. Built in validators cover most needs so you avoid writing lots of repetitive XML files.

Quick checklist to get started

  • Add validation annotations to getters or fields in your action class
  • Enable annotation processing in your Struts setup by adding the validator module or plugin and turning on scanning
  • Prefer built in validators for common checks and write a custom validator when the rules get weird
  • Handle errors with field messages and map the failure result back to the original form page
  • Test with unit tests and a few manual cases so users do not file bug reports that read like horror stories

Step 1 Add annotations near properties

Put annotations on the getter or the field so the rule lives with the code it protects. Reviewers and future you will thank you. Example for a required email field that also enforces format.

@RequiredStringValidator(fieldName="email", message="Email required")
@EmailValidator(fieldName="email", message="Email format invalid")
public String getEmail() {
    return email
}

Step 2 Turn on annotation scanning in your project

Most projects only need to add the validation support library to the classpath and enable annotation processing in the Struts configuration. The framework will then scan action classes for validation metadata and run those validators during the Validation phase. If your build system needs a dependency update then add the validation module that matches your Struts version.

Step 3 Use built in validators or write a custom one

Built in validators cover required values length regex and email checks for most forms. When business rules get quirky implement a custom validator class by extending FieldValidatorSupport and overriding the validate method. Keep complex logic out of your action methods and out of the view.

public class MyCustomValidator extends FieldValidatorSupport {
    @Override
    public void validate(Object object) {
        // perform validation and call addFieldError if needed
    }
}

Step 4 Report errors in a user friendly way

Use addFieldError to attach messages to specific fields and map a failure result back to the original form page. Showing a friendly message and focusing the first invalid field will cut down on help desk tickets and passive aggressive emails.

Step 5 Test the rules

Write unit tests that exercise your action validation and add a few manual test cases that mimic edge case input. Automated tests help catch regressions faster than letting QA or users file bug reports. Adjust message text and severity based on user feedback.

Best practices and tips

  • Prefer annotations for small to medium forms because proximity improves readability
  • Use built in validators where possible to avoid custom maintenance
  • When you must write a custom validator keep it focused single responsibility and covered by tests
  • Keep error messages actionable and non judgemental so users do not feel like they failed at life

This walkthrough covered how to add annotation based validators to Struts action classes enable the validator subsystem choose between built in and custom validators manage error feedback and test validation rules so forms behave predictably and code stays maintainable. Now go fix those forms and enjoy slightly fewer support tickets.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.