Thymeleaf报错:找不到bean名称'emp'的请求属性问题求助
问题:Thymeleaf添加员工时出现
emp对象缺失错误 最近在开发Thymeleaf项目,已经实现员工列表展示、部门选择以及员工删除功能,现在要实现向指定部门添加员工的功能,但遇到了超出预期的问题。我尝试创建新的Employee对象并加入模型,再通过输入框录入员工信息,但emp对象似乎缺失,具体错误信息如下:
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'emp' available as request attribute
我的Controller类代码:
@Controller @Slf4j @SessionAttributes({"department"}) @RequestMapping("/department") public class DepartmentController { @Autowired private DepartmentRepository departmentRepository; @Autowired private EmployeeRepository employeeRepository; @PostConstruct public void initDB(){ Path path = Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "data.json"); ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); List<Department> departments = null; try { departments = mapper.readValue(path.toFile(), new TypeReference<List<Department>>() {}); if(departments != null){ for(Department department : departments){ department.getEmployees().forEach(e -> e.setDepartment(department)); } departmentRepository.saveAll(departments); } } catch (IOException e) { throw new RuntimeException(e); } } @GetMapping public String getDepartments(Model model){ List<Department> departments = departmentRepository.findAll(); model.addAttribute("depts", departments); return "departmentView"; } @PostMapping public String getEmployeesOfDepartment(Model model, @RequestParam("deptName") String departmentName){ Department department = departmentRepository.getDepartmentByDeptNo(departmentName); updateModel(model, department); return "departmentView"; } @PostMapping("/remove/{empId}") public String removeEmployeeFromDepartment(Model model, @SessionAttribute("department") Department department, @PathVariable("empId") Integer empId) { Employee employee = employeeRepository.findByEmployeeNo(empId); department.getEmployees().remove(employee); employeeRepository.delete(employee); departmentRepository.flush(); updateModel(model, department); return "departmentView"; } @PostMapping("/create") public String addEmployee(Model model, @SessionAttribute("department") Department department, @ModelAttribute("emp") Employee employee){ department.addEmployee(employee); employee.setDepartment(department); employeeRepository.save(employee); updateModel(model, department); return "departmentView"; } public void updateModel(Model model, Department department){ model.addAttribute("depts", departmentRepository.findAll()); model.addAttribute("department", department); model.addAttribute("selectedDepartment", department.getDeptName()); model.addAttribute("manager", department.getDeptManager()); model.addAttribute("emp", new Employee()); model.addAttribute("employees", department.getEmployees()); } }
我的department模板代码:
<!DOCTYPE html> <html lang="en" xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Departments</title> </head> <body> <h1>Department</h1> <form method="post" th:action="@{/department}"> <label for="choice"></label> <select id="choice" name="deptName" onchange="submit();"> <option th:each="department : ${depts}" th:value="${department.deptNo}" th:text="${department.deptName}" th:selected="${department.deptName == selectedDepartment}"></option> </select> </form> <table> <form method="post"> <tr th:if="${manager != null}"> <td th:value="${manager.employeeNo}" th:text="${manager.employeeNo}"></td> <td th:value="${manager.firstName}" th:text="${manager.firstName}"></td> <td th:value="${manager.lastName}" th:text="${manager.lastName}"></td> <td th:value="${manager.gender}" th:text="${manager.gender}"></td> </tr> </form> <form method="post" th:each="employee : ${employees}"> <tr> <td th:value="${employee.employeeNo}" th:text="${employee.employeeNo}"></td> <td th:value="${employee.firstName}" th:text="${employee.firstName}"></td> <td th:value="${employee.lastName}" th:text="${employee.lastName}"></td> <td th:value="${employee.gender}" th:text="${employee.gender}"></td> <td> <form method="post"> <input type="submit" value="Remove" th:formaction="@{/department/remove/{empId}(empId=${employee.employeeNo})}"> </form> </td> </tr> </form> </table> <form method="get"> <button>Add Employee</button> </form> <form method="post" th:action="@{/department/create}" th:object="${emp}"> <label th:for="employeeNo">Employee Number: </label> <input type="text" th:field="*{employeeNo}"> <br> <label th:for="firstname">First Name: </label> <input th:field="*{firstname}" type="text"> <br> <label th:for="lastname">Last Name: </label> <input th:field="*{lastname}" type="text"> <br> <label th:for="gender">Gender: </label> <input th:field="*{gender}" type="text"> <br> <button onclick="submit();">Add Employee</button> </form> </body> </html>
我已经试过YouTube教程、Baeldung文章、Thymeleaf官方文档,甚至咨询过ChatGPT,但问题仍未解决,希望能得到帮助。
内容的提问来源于stack exchange,提问作者Gahatma
相关产品推荐
相关产品推荐

