gpt4 book ai didi

java - Spring @Valid 无法在 Post Controller 中正常工作(多部分)

转载 作者:行者123 更新时间:2023-11-30 06:11:12 25 4
gpt4 key购买 nike

我有一个表单,应该上传带有 @ModelAttribute 的图像。当我尝试验证该对象中的某些约束时,就会出现问题。发生的情况是 Controller 方法代码不执行,我只是抛出异常约束

Field error in object 'employee' on field 'details.name': rejected value []; codes [Size.employee.details.name,Size.details.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.name,details.name]; arguments []; default message [details.name],20,1]; default message [*Must be between 1 and 20 letters]
Field error in object 'employee' on field 'details.lastName': rejected value []; codes [NotBlank.employee.details.lastName,NotBlank.details.lastName,NotBlank.lastName,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.lastName,details.lastName]; arguments []; default message [details.lastName]]; default message [*Please provide a valid last name]
Field error in object 'employee' on field 'details.name': rejected value []; codes [NotBlank.employee.details.name,NotBlank.details.name,NotBlank.name,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.name,details.name]; arguments []; default message [details.name]]; default message [*Please provide a valid first name]
Field error in object 'employee' on field 'jobOccupation': rejected value []; codes [NotBlank.employee.jobOccupation,NotBlank.jobOccupation,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.jobOccupation,jobOccupation]; arguments []; default message [jobOccupation]]; default message [*Employee must have an occupation]
Field error in object 'employee' on field 'details.lastName': rejected value []; codes [Size.employee.details.lastName,Size.details.lastName,Size.lastName,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.lastName,details.lastName]; arguments []; default message [details.lastName],20,1]; default message [*Must be between 1 and 20 letters]

如果所有字段都已填充(不包括图像,因为它没有任何约束)一切正常,但我需要验证。

这是我的表格

<form class="mar-clearfix"  th:action="@{/admin/staff/create/employee}" id="emp-form"
th:object="${employee}" enctype="multipart/form-data"
method="post">
<div class="col s12 xl6">
<div class="field-wrapper">
<p th:if="${#fields.hasErrors('details.name')}" th:errors="*{details.name}" class="error"></p>
<div class="input-field">
<input type="text" th:field="*{details.name}" id="emp-f-name">
<label for="emp-f-name">First Name</label>
</div>
</div>
<div class="field-wrapper">
<p th:if="${#fields.hasErrors('details.lastName')}" th:errors="*{details.lastName}" class="error"></p>
<div class="input-field">
<input type="text" th:field="*{details.lastName}" id="emp-l-name">
<label for="emp-l-name">Last Name</label>
</div>
</div>
<div class="field-wrapper">
<div class="input-field">
<input type="email" th:field="*{email}" id="emp-email">
<label for="emp-email">Email</label>
</div>
</div>
<div class="field-wrapper">
<div class="input-field">
<label for="emp-birthday">Birthday</label>
<input type="text" id="emp-birthday" th:field="*{details.dateOfBirth}" class="datepicker">
</div>
</div>
</div>
<div class="col s12 xl6">
<div class="field-wrapper">
<p th:if="${#fields.hasErrors('jobOccupation')}" th:errors="*{jobOccupation}" class="error"></p>
<div class="input-field">
<input type="text" th:field="*{jobOccupation}" id="emp-job">
<label for="emp-job">Job Ocupation</label>
</div>
</div>
<div class="field-wrapper">
<div class="input-field">
<select th:field="*{details.gender}">
<option th:value="${T(com.mypackage.Genders).MALE}">Male</option>
<option th:value="${T(com.mypackage.Genders).FEMALE}">Female</option>
</select>
</div>
</div>
<div class="field-wrapper" style="margin-top:23px">
<div class="input-field">
<select th:field="*{roles}" multiple>
<option th:value="null" disabled selected>Select roles</option>
<option th:each="item : ${roles}" th:value="${{item}}"
th:text="${item.role}" ></option>
</select>
</div>
</div>
<div class="field-wrapper" style="margin-top:23px">
<div class="file-field input-field">
<div class="btn">
<span>Profile img</span>
<input type="file" name="image">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
</div>
</form>

Controller 方法

@PostMapping(value = "/admin/staff/create/employee")
public ModelAndView createEmployee(@ModelAttribute ("employee") @Valid Employee employee
,@RequestPart("image")MultipartFile image, BindingResult result) {
ModelAndView mav = new ModelAndView();
mav.setViewName("admin/admin_layout");
mav.addObject("page", "employee_create");
mav.addObject("roles",service.getEmployeeRoles());
System.out.println("saving");
if(result.hasErrors()) {
System.out.println("has errors");
return mav;}}

我尝试在@ModelAttribute之前添加@RequestPart,但这似乎没有帮助。此外,当我从表单中删除 @RequestPart 图像和 enctype 时,一切正常,错误会正确写入相应的 th:error 字段。我的问题是,为什么在尝试使用表单上传图像时会发生这种情况,有什么办法可以解决吗?谢谢。

最佳答案

使用 Validator bean 在 Controller 方法内运行验证:

public class MyController
/**
* The system wide JSR-303 validator, which checks if annotations ({@link NotNull}, {@link NotBlank}, etc. ) are fulfilled.
*/
@Autowired
protected Validator validator;

@PostMapping(value = "/admin/staff/create/employee")
public ModelAndView createEmployee(@ModelAttribute ("employee") Employee employee, @RequestPart("image")MultipartFile image, BindingResult result) {

validator.validate(employee, result);

ModelAndView mav = new ModelAndView();
mav.setViewName("admin/admin_layout");
...
if (result.hasErrors()) {
System.out.println("has errors");
return mav;
}
}
}

关于java - Spring @Valid 无法在 Post Controller 中正常工作(多部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50194926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com