Home > Java > javaTutorial > body text

How do you validate user input in Spring MVC?

Susan Sarandon
Release: 2024-10-29 18:30:02
Original
904 people have browsed it

How do you validate user input in Spring MVC?

Validating User Inputs in Spring MVC: A Thorough Guide

Validating user inputs is crucial for ensuring the integrity and reliability of form data in web applications. Spring MVC provides several approaches to accomplish this task, each with its advantages and drawbacks.

Method 1: Annotation-Based Validation

For simple validation requirements, Spring 3.x and later introduces the use of javax.validation.constraints annotations. These annotations are applied directly to bean properties, marking them as required or subject to specific constraints. For example:

<code class="java">public class User {

    @NotNull
    private String name;

    ...

}</code>
Copy after login

In your controller, you can utilize @Valid and @ModelAttribute to perform validation:

<code class="java">@RequestMapping(value="/user", method=RequestMethod.POST)
public createUser(Model model, @Valid @ModelAttribute("user") User user, BindingResult result){
    if (result.hasErrors()){
      // do something
    }
    else {
      // do something else
    }
}</code>
Copy after login

Method 2: Manual Validation

For more complex validation requirements, manually implementing validation logic using the org.springframework.validation.Validator interface is recommended.

<code class="java">public class UserValidator implements Validator {

    @Override
    public boolean supports(Class clazz) {
      return User.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
      User user = (User) target;

      if(user.getName() == null) {
          errors.rejectValue("name", "your_error_code");
      }

      // do "complex" validation here

    }

}</code>
Copy after login

In your controller:

<code class="java">@RequestMapping(value="/user", method=RequestMethod.POST)
public createUser(Model model, @ModelAttribute("user") User user, BindingResult result){
    UserValidator userValidator = new UserValidator();
    userValidator.validate(user, result);

    if (result.hasErrors()){
      // do something
    }
    else {
      // do something else
    }
}</code>
Copy after login

Method 3: Combined Approach

Combining both annotation-based and manual validation can leverage the advantages of both approaches.

Warning:

Validation handling should not be confused with exception handling. Validation pertains to the business rules and data constraints of your application, while exception handling addresses system errors.

References:

  • [Blog post about Bean Validation](https://dzone.com/articles/bean-validation-without-ejb)
  • [Blog post about Validation with Spring MVC](https://www.baeldung.com/spring-mvc-validation)
  • [Spring Documentation on Validation](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/annotation/package-summary.html)

The above is the detailed content of How do you validate user input in Spring MVC?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template