上一篇文章已经介绍了首页和员工展示页,接下来完成新增、更新和删除员工的功能。新增按钮的代码:
<!-- align-self-center使其垂直居中 -->
<div class="col align-self-center">
<a class="btn btn-sm btn-success" th:href="@{/toAdd}">添加员工</a>
</div>
处理toAdd请求的方法:
@RequestMapping("/toAdd")
public String toAddPage(Model model) {
model.addAttribute("departments", departmentDao.getAllDepartments());
return "emps/add";
}
因为新增员工页面有部门的下拉框,因此需要将所有的部门信息传给该页面。新增员工的页面add.html的主要代码:
<form class="mt-3" action="/add">
<div class="row form-row">
<div class="col form-group">
<label for="inputUserName">姓名</label>
<input type="text" class="form-control" id="inputUserName" name="name" required>
</div>
<div class="col form-group">
<label for="inputAge">年龄</label>
<input type="number" class="form-control" id="inputAge" name="age" required>
</div>
</div>
<div class="form-row">
<div class="col form-group align-self-center">
<label>性别</label>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="inlineCheckbox1" value="1" name="sex" required>
<label class="form-check-label" for="inlineCheckbox1">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="inlineCheckbox2" value="0" name="sex" required>
<label class="form-check-label" for="inlineCheckbox2">女</label>
</div>
</div>
<div class="col form-group">
<label for="inputDepartment">部门</label>
<select class="form-control" id="inputDepartment" name="department.id">
<option th:each="dep:${departments}" th:text="${dep.getName()}" th:value="${dep.getId()}"></option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputBirthday">生日</label>
<input type="date" class="form-control" id="inputBirthday" name="birthday">
</div>
</div>
<!-- 水平居中 -->
<div class="row justify-content-center">
<button type="submit" class="btn btn-primary align-self-center">Submit</button>
<div class="col-md-1"></div>
<a class="btn btn-primary align-self-center" th:href="@{/emps}">Cancel</a>
</div>
</form>
为了能让后端接收到一个Employee对象,各个输入框的name属性需要与Employee的属性名一致,其中部门输入框传给后端对应的是department的id,其值为dep.getId(),其展示的文本为dep.getName()。而日期输入框传给后端为字符串'yyyy-MM-dd'的格式,正常情况下不能转化为Date对象,所以需要给Employee的birthday(Date类型)属性加上格式注解:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
这样就能进行自动转化前端的数据为Employee对象而不报错了。
新增和删除的按钮在表格中:
<td>
<!-- 注意a标签链接的restful风格传参方式 -->
<a class="btn btn-sm btn-primary" th:href="@{'/toUpdate/' + ${emp.getId()}}">编辑</a>
<a class="btn btn-sm btn-danger" th:href="@{'/delete/' + ${emp.getId()}}">删除</a>
</td>
注意这两个功能需要传入员工的id,使用restful风格,传参方式先写请求名,即toUpdate或者delete,由单引号包起来,再用加号拼接${emp.getId()}。以下是处理toUpdate请求的后端代码:
@RequestMapping("/toUpdate/{id}")
// 使用restful风格传参,要在参数前加上@PathVariable的注解
public String toUpdatePage(@PathVariable int id, Model model) {
model.addAttribute("emp", employeeDao.getEmployeeById(id));
model.addAttribute("departments", departmentDao.getAllDepartments());
return "emps/update";
}
更新页面带有该员工的信息,所以需要根据员工id把员工信息查出来,与新增页面类似,也需要查出所有部门信息传给更新页面。更新页面update.html的主要代码如下:
<form class="mt-3" action="/update">
<div class="form-group">
<input hidden="hidden" type="number" class="form-control" id="id" name="id" th:value="${emp.getId()}" required>
</div>
<div class="row form-row">
<div class="col form-group">
<label for="inputUserName">姓名</label>
<input type="text" class="form-control" id="inputUserName" name="name" th:value="${emp.getName()}" readonly="readonly" required>
</div>
<div class="col form-group">
<label for="inputAge">年龄</label>
<input type="number" class="form-control" id="inputAge" name="age" th:value="${emp.getAge()}" required>
</div>
</div>
<div class="form-row">
<div class="col form-group align-self-center">
<label>性别</label>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="inlineCheckbox1" value="1" name="sex" th:checked="${emp.getSex()==1}" required>
<label class="form-check-label" for="inlineCheckbox1">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="inlineCheckbox2" value="0" name="sex" th:checked="${emp.getSex()==0}" required>
<label class="form-check-label" for="inlineCheckbox2">女</label>
</div>
</div>
<div class="col form-group">
<label for="inputDepartment">部门</label>
<select class="form-control" id="inputDepartment" name="department.id">
<option th:selected="${dep.getId()==emp.getDepartment().getId()}" th:each="dep:${departments}" th:text="${dep.getName()}" th:value="${dep.getId()}"></option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputBirthday">生日</label>
<input type="date" class="form-control" id="inputBirthday" name="birthday" th:value="${#dates.format(emp.getBirthday(), 'yyyy-MM-dd')}">
</div>
</div>
<!-- 水平居中 -->
<div class="row justify-content-center">
<button type="submit" class="btn btn-primary align-self-center">Submit</button>
<div class="col-md-1"></div>
<button class="btn btn-primary align-self-center" th:href="@{/emps}">Cancel</button>
</div>
</form>
其中每一个输入框都需要预先将数据预制进去,一般由th:value来完成,其中性别单选框需要使用th:checked+三目表达式来确定: th:checked="${emp.getSex()==1}",下拉框需要使用th:selected+三目表达式来确定:th:selected="${dep.getId()==emp.getDepartment().getId()}",生日则需要转换格式:th:value="${#dates.format(emp.getBirthday(), 'yyyy-MM-dd')}"。
删除按钮的功能则完全交给后端,删完后重定向到list.html页面,即/emps请求:
@RequestMapping("/delete/{id}")
public String delete(@PathVariable int id){
employeeDao.deleteEmployee(id);
return "redirect:/emps";
}