Spring Controller中BindingResult的Driver对象赋值来源咨询
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:- Create an instance of
Driverfor you. - Pull values from incoming HTTP request parameters that match
Driver's property names (e.g., a request parameterdriverIdmaps todriver.setDriverId(...)). - Assign these values to the
Driverobject. - Collect any binding errors (like type mismatches, missing required fields, etc.) into the
BindingResultparameter that directly follows theDriverparameter in the method signature.
- Create an instance of
- Critical note: The order matters!
BindingResultmust 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

