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

Spring Controller中BindingResult的Driver对象赋值来源咨询

Understanding How Spring Binds Driver to BindingResult & Binding Other Objects for Validation

Hey there! Since you're new to Spring Framework, let me break this down clearly for you.


How Does Driver Get Bound to BindingResult?

This all works thanks to Spring MVC's automatic request data binding mechanism:

  • When you declare a model object (like Driver) as a parameter in your Controller method, Spring will automatically:
    1. Create an instance of Driver for you.
    2. Pull values from incoming HTTP request parameters that match Driver's property names (e.g., a request parameter driverId maps to driver.setDriverId(...)).
    3. Assign these values to the Driver object.
    4. Collect any binding errors (like type mismatches, missing required fields, etc.) into the BindingResult parameter that directly follows the Driver parameter in the method signature.
  • Critical note: The order matters! BindingResult must sit immediately after the model object it's linked to—if you put other parameters in between, Spring won't associate the errors with that object.

Your Controller method likely looks something like this under the hood:

@PostMapping("/driver/saveStatusSchedule")
public String saveStatusSchedule(Driver driver, BindingResult bindingResult) {
    // Check for binding/validation errors
    if (bindingResult.hasErrors()) {
        // Return to the form page to display errors
        return "your-form-view";
    }
    // Process the valid Driver object
    return "success-view";
}

How to Bind & Validate Other Objects

If you need to bind and validate other objects (not just Driver), follow these simple steps:

1. Add Validation Rules to Your Object

Use JSR-380 (Bean Validation) annotations on your object's fields to define validation rules. For example:

public class YourCustomObject {
    @NotNull(message = "This field can't be empty")
    @Size(min = 2, max = 50, message = "Name must be 2-50 characters long")
    private String objectName;

    @Min(value = 18, message = "Age must be at least 18")
    private int age;

    // Getters and Setters are required for Spring to bind values!
}

2. Update Your Controller Method

Add the @Valid (or @Validated for group validation) annotation to your object parameter, followed by a BindingResult to capture errors:

@PostMapping("/your-submit-url")
public String handleCustomObjectSubmit(
        @Valid @ModelAttribute("customObject") YourCustomObject customObject,
        BindingResult bindingResult
) {
    if (bindingResult.hasErrors()) {
        // Errors exist—return to the form page to show them
        return "custom-form-view";
    }
    // Validation passed—process the object
    return "success-view";
}

3. Adjust Your Thymeleaf Form

Update the th:object attribute to reference your new object, and use th:field to bind form inputs to its properties:

<form id="customForm" class="form-horizontal" 
      th:action="@{/your-submit-url}" 
      th:method="POST" 
      th:object="${customObject}">
    <div class="form-group">
        <label>Object Name</label>
        <input type="text" class="form-control" th:field="*{objectName}"/>
        <!-- Show validation error for this field -->
        <span th:if="${#fields.hasErrors('objectName')}" th:errors="*{objectName}" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label>Age</label>
        <input type="number" class="form-control" th:field="*{age}"/>
        <span th:if="${#fields.hasErrors('age')}" th:errors="*{age}" class="text-danger"></span>
    </div>
    <button type="submit">Submit</button>
</form>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:56:31