You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring项目校验配置异常求助:bindingResult.hasErrors()始终返回false

解决Spring校验无法触发的问题

以下是导致校验逻辑不生效的核心问题及修复方案:


1. 修复JSP表单标签拼写错误

你的JSP中使用了错误的标签<from:form>,正确的Spring表单标签应该是<form:form>。拼写错误会导致表单无法正确提交参数,校验逻辑自然不会触发。

修正后的JSP代码:

<form: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">
</form:form>

2. 修正实体类的校验注解与依赖

Hibernate Validator 8.x基于Jakarta Validation 3.0,不再支持旧的javax.validation包,同时实体类必须提供getter/setter方法(Spring MVC需要通过这些方法绑定请求参数)。

修正后的实体类:

import jakarta.validation.constraints.*;

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;

    // 必须添加getter/setter,否则Spring无法绑定参数
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}

3. 调整控制器方法参数

控制器中同时存在BindingResult和Errors参数会导致校验结果传递异常,且@Valid注解需要从jakarta.validation包导入。

修正后的控制器代码:

import jakarta.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.Controller;

@Controller
@RequestMapping("/")
public class MyController {

    @RequestMapping("/showDetails")
    public String showDetails(
            @ModelAttribute("employee") @Valid Employee employee,
            BindingResult bindingResult) { // 仅保留BindingResult,且必须紧跟@Valid参数

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

4. 完善Maven依赖

Hibernate Validator 8需要搭配Jakarta Validation API才能正常工作,补充依赖如下:

<!-- Jakarta Validation API(必填) -->
<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>3.0.2</version>
    <scope>provided</scope>
</dependency>

<!-- Hibernate Validator 实现 -->
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>8.0.1.Final</version>
</dependency>

5. 启用Spring MVC注解驱动

确保你的Spring MVC配置开启了注解驱动,这是触发校验逻辑的核心配置:

  • Java配置类:添加@EnableWebMvc注解
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    @Configuration
    @EnableWebMvc
    public class WebConfig {
        // 其他配置
    }
    
  • XML配置:添加<mvc:annotation-driven/>标签
    <beans xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc
                               https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:annotation-driven/>
    </beans>
    

内容的提问来源于stack exchange,提问作者Tima Khamza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 15:53:26