【发布时间】:2018-03-22 13:59:18
【问题描述】:
我正在尝试验证表单。 Spring 正确验证,但是当应该返回错误时,它会收到一个异常:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute
添加我不知道出了什么问题。你能帮帮我吗?
控制器:
@RequestMapping(value = "/employee/add",method = RequestMethod.POST)
public String addEmployee(@Valid DTOEmployee dtoEmployee, BindingResult result) {
if (result.hasErrors()) {
return "employee_add";
}
employeeService.save(dtoEmployee);
return "redirect:/employee?add";
}
DTO员工:
public class DTOEmployee{
@NotNull
private String name;
@NotNull
private String subname;
@NotNull
private String email;
}
employee_add 的片段:
<form th:action="@{/employee/add}" th:object="${employee}" method="post" class="form-inline justify-content-center">
<div class="input-group my-1">
<input th:field="*{name}" type="text" id="name" class="form-control"
placeholder="Your name"/>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</p>
<input th:field="*{subname}" type="text" id="name" class="form-control"
placeholder="Your subname"/>
<p th:if="${#fields.hasErrors('subname')}" th:errors="*{name}">Name error</p>
<input th:field="*{email}" type="email" id="email" class="form-control"
placeholder="Your mail"/>
<p th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name error</p>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
messages.properties:
NotNull.DTOEmployee.name = Name must be not null
NotNull.DTOEmployee.subname= Subname must be not null
NotNull.DTOEmployee.email= Email must be not null
【问题讨论】:
标签: spring validation thymeleaf