问题
Just faced a wired change of behavior after upgrading the Spring Boot version from 2.3.0 to 2.3.1
There is such a simple Thymleaf view with multi-select control
<div class="form-group">
<label for="roles">Roles</label>
<select multiple class="form-control" id="roles" th:field="*{roles}" required>
<option th:each="role : ${roles}"
th:text="${role.name}"
th:value="${role.id}"
th:selected="${user.roles?.contains(role)}"></option>
</select>
</div>
and POST handling controller method
@PostMapping
public String saveUser(@Valid User user, BindingResult bindingResult) {
logger.info("Save user method");
userService.save(user);
return "redirect:/user";
}
With version 2.3.0 if I submit the form with some roles selected I got them as a correct set of roles in roles field.
But with version 2.3.1 I got an incorrect list of roles. Id field in role object is empty in all of them and the name field is filled with id value. Looks like some Spring MVC or Thymleaf configuration is changed in this version but I see nothing obvious in the release notes here https://github.com/spring-projects/spring-boot/releases/tag/v2.3.1.RELEASE
I understand that this problem could be resolved by implementing custom Formatter but why it works without it with Spring Boot 2.3.0.
User class is like that
public class User {
private Long id;
private String name;
private String password;
private Set<Role> roles;
// getters, setters, etc.
}
Role class
public class Role implements Serializable {
private Long id;
private String name;
// getters, setters, equals, hashcode, etc.
}
来源:https://stackoverflow.com/questions/62644051/change-of-multiple-select-handling-in-spring-boot-2-3-1