- 提交按钮
<!-- 设置action,进入对应的controller 请求方法:post -->
<form th:action="@{/emp}" method="post">
//进入@PostMapping的controller
@PostMapping("/emp")
public String addEmp(Employee employee){
System.out.println(employee);
employeeDao.add(employee);
return "redirect:/emps";
}
- 跳转到添加页面
<!-- 点击添加添加员工,进行页面跳转 -->
<h2><a class="btn btn-sm btn-success" th:href="emp">添加员工</a></h2>
//对应的controller,@GetMapping,此controller进行页面的跳转
//model设置departments参数返回前端显示
@GetMapping("/emp")
public String toAddPage(Model model){
Collection<Department> department = departmentDao.getDepartment();
model.addAttribute("departments",department);
return "emp/add";
}
<!-- 前端遍历departments进行显示 -->
<select class="form-control" name="department.id">
<option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
</select>
- 添加员工成功
- 返回首页
@PostMapping("/emp")
public String addEmp(Employee employee){
System.out.println(employee);
employeeDao.add(employee);
//重定向到员工信息页面,
return "redirect:/emps";
}
//跳转到对应这,返回list页面。
@RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeDao.getAllEmployee();
model.addAttribute("emps",employees);
return "emp/list";
}