Spring @Valid在后置控制器(多部分)中无法正常工作



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

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>

控制器方法

@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 字段中。我的问题是为什么在尝试上传带有表单的图像时会发生这种情况,有什么办法可以解决吗?谢谢。

使用Validatorbean 在控制器方法中运行验证:

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;
}
}
}

最新更新