org.springframework.validation.BeanPropertyBindingResult:0 个错误

问题描述 投票:0回答:1

有人可以帮忙解决这个问题吗? 我无法在 spring 项目中配置验证。

实体类

public class Employee {
    @Size(min = 2,message = "Name must be minimum 2 symbols")
    private String name;

    @NotEmpty(message = "surname is required field")
    private String surname;
}

控制器类

@RequestMapping("/showDetails")
    public String showDetails(
            @ModelAttribute("employee") @Valid Employee employee,
            BindingResult bindingResult, Errors errors){

     
        if(bindingResult.hasErrors()){   // bindingResult.hasErrors() always return false
            return "ask-detail-view";
        }else{
            return "show-detail-view";
        }

    }

pom.xml

<dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>8.0.1.Final</version>
    </dependency>

jsp页面


<from:form action="showDetails" modelAttribute="employee">
    Name<form:input path="name"/>
    <form:errors path="name" />
    <br>
    <br>
    Surname<form:input path="surname"/>
    <form:errors path="surname" />
    <input type="submit">
</from:form>

如何正确配置?

java spring spring-mvc
1个回答
0
投票

@Valid注解已经提供了相应的响应。因此,如果您不需要函数中的确切错误,这应该足够了。

© www.soinside.com 2019 - 2024. All rights reserved.