[英]Spring Boot CRUD Application with Thymeleaf - BindingResult result
I have a SpringBoot app with this bean:我有一个带有这个 bean 的 SpringBoot 应用程序:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
public class UserPayload implements Serializable {
private Long id;
@NotEmpty
private String password;
@NotEmpty
private String confirmPassword;
@NotBlank(message = "Name is mandatory")
@NotEmpty
@NotNull
private String name;
...
}
and the controller:和 controller:
@PostMapping("/adduser.html")
public String addUser(@Valid UserPayload userPayload,
BindingResult result,
Model model,
@RequestParam("files") MultipartFile[] multipartFiles) {
...
}
on the template:在模板上:
<form action="#" th:action="@{/adduser.html}" th:object="${user}" method="post" enctype="multipart/form-data">
<input type="text" id="name" class="form-control" th:field="*{name}" placeholder="Introduce you name">
...
</form>
but BindingResult result shows 0 errors when all the fields are empty但是当所有字段为空时,BindingResult 结果显示 0 错误
Your problem could be motivated for different reasons.你的问题可能出于不同的原因。
One thing you can try is to annotate the UserPayload
object with a ModelAttribute
annotation:您可以尝试的一件事是使用 ModelAttribute 注释对
UserPayload
ModelAttribute
进行注释:
@PostMapping("/adduser.html")
public String addUser(@ModelAttribute("userPayload") @Valid UserPayload userPayload,
BindingResult result,
Model model,
@RequestParam("files") MultipartFile[] multipartFiles) {
...
}
Perhaps it could be of help as it seems you are also providing the files
parameter.也许它可能会有所帮助,因为您似乎还提供了
files
参数。 Please, adjust the model attribute name as appropriate.请适当调整 model 属性名称。
Please, also verify that you have an implementation of the Validation API in your maven dependencies, not only the java.validation
API, like hibernate-validator
: Please, also verify that you have an implementation of the Validation API in your maven dependencies, not only the
java.validation
API, like hibernate-validator
:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.3.Final</version>
</dependency>
Please, set the right version according with the rest of libraries in your project: if you are using Spring dependency management you can safely rely on it.请根据您项目中库的 rest 设置正确的版本:如果您使用的是 Spring 依赖管理,您可以放心地依赖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.