SpringMVC data verification method based on Validator interface developed by java

  • 2021-11-13 07:39:25
  • OfStack

Directory 1. Define Entity Class Account2. Custom Verifier AccountValidator to Implement Validator Interface 3. Controller 4. springmvc. xml Configuration Verifier 5. jsp File

Spring MVC provides two ways to verify data:

1. Based on Validator interface.

2. Use Annotation JSR-303 standard for verification.

The Validator validator needs to be customized based on Validator interface, and the validation rules of each piece of data need to be completed by developers. The Annotation JSR-303 standard does not need to define the validator, and the validation rules of each attribute can be directly added to the entity class by annotation, which is more convenient and recommended in actual development.

1. Define Entity Class Account


package entity;
import lombok.Data;
@Data
public class Account {
    private String name;
    private String password;
}

2. Custom validator AccountValidator to implement Validator interface


package validator;
import entity.Account;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class AccountValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return Account.class.equals(aClass);
    }
    @Override
    public void validate(Object o, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors,"name",null," Name cannot be empty ");
        ValidationUtils.rejectIfEmpty(errors,"password",null," Password cannot be empty ");
    }
}

There are two methods in Validator, supports is to judge whether the incoming is the target class, and if so, carry out the next step, data verification.

3. Controller


package Mycontroller; 
import entity.Account;
import entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
@Controller
@RequestMapping("/validator")
public class ValidatorHandler {
    @GetMapping("/login")
    public String login(Model model){
        model.addAttribute("account",new Account());
        return "login";
    }
    @PostMapping("/login")
    public String login(@Validated Account account, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return "login";
        }
        return "index";
    }

4. springmvc. xml Configuration Verifier


<bean id="accountValidator" class="com.southwind.validator.AccountValidator">
</bean>
<mvc:annotation-driven validator="accountValidator"></mvc:annotation-driven>

5. jsp file


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form:form modelAttribute="account" action="/validator/login" method="post">
         Name :<form:input path="name"></form:input><form:errors path="name"></form:errors><br>
         Password :<form:input path="password"></form:input><form:errors path="password"></form:errors><br>
        <input type="submit" value=" Login ">
    </form:form>
</body>
</html>

The above is the SpringMVC data verification method developed by java based on Validator interface. For more information about SpringMVC data verification, please pay attention to other related articles on this site!


Related articles: