我是靠谱客的博主 贤惠冬天,这篇文章主要介绍Spring boot修改员工,现在分享给大家,希望可以做个参考。

修改员工

点击修改按钮,根据用户id
查询用户信息,查询所有的部门列表信息

回显到修改页面
点击确认,提交用户信息

用户列表页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"> <h2><a class="btn btn-sm btn-success" href="emp" th:href="@{/emp}">员工添加</a></h2> <div class="table-responsive"> <table class="table table-striped table-sm"> <thead> <tr> <th>#</th> <th>lastName</th> <th>email</th> <th>gender</th> <th>department</th> <th>birth</th> <th>操作</th> </tr> </thead> <tbody> <tr th:each="emp:${emps}"> <td th:text="${emp.id}"></td> <td>[[${emp.lastName}]]</td> <td th:text="${emp.email}"></td> <td th:text="${emp.gender}==0?'女':'男'"></td> <td th:text="${emp.department.departmentName}"></td> <td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td> <td> <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a> <button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除 </button> </td> </tr> </tbody> </table> </div> </main>

Controller

根据id,查询用户信息
查询所有部门信息,返回修改页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
//来到修改页面,查出当前员工,在页面回显 @GetMapping("/emp/{id}") public String toEditPage(@PathVariable("id") Integer id, Model model) { Employee employee = employeeDao.get(id); model.addAttribute("emp", employee); //页面要显示所有的部门列表 Collection<Department> departments = departmentDao.getDepartments(); model.addAttribute("depts", departments); //回到修改页面(add是一个修改添加二合一的页面); return "emp/add"; }

回显用户信息

复制代码
1
<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">

回显选中部门
如果,当前部门id,等于用户的部门id

th:selected

设置为选中

复制代码
1
2
3
4
<!--提交的是部门的id--> <select class="form-control" name="department.id"> <option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option> </select>

出生日期

#dates.format

格式化为指定日期格式

复制代码
1
<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">

三元运算符
添加用户、修改用户共用一个页面

添加的时候,emp为空
修改的时候,emp不为空,显示用户信息

复制代码
1
th:checked="${emp!=null}?${emp.gender==1}

提交按钮
添加的时候,显示添加
修改的时候,显示修改

复制代码
1
<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>

点击添加
设置发送put请求

还是会执行form表单的action请求
提交方式,使用配置的put方式提交

复制代码
1
<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>

Controller

保存员工信息

复制代码
1
2
3
4
5
6
7
//员工修改;需要提交员工id; @PutMapping("/emp") public String updateEmployee(Employee employee) { System.out.println("修改的员工数据:" + employee); employeeDao.save(employee); return "redirect:/emps"; }

员工id
使用隐藏域

当添加时,emp为null,不生成input标签
用户的id,在后台自动的生成id

当修改emp不为null时,生成input标签
Name为id,value为修改用户id

复制代码
1
<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">

公共页面

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"> <!--需要区分是员工修改还是添加;--> <form th:action="@{/emp}" method="post"> <!--发送put请求修改员工数据--> <!-- 1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的) 2、页面创建一个post表单 3、创建一个input项,name="_method";值就是我们指定的请求方式 --> <input type="hidden" name="_method" value="put" th:if="${emp!=null}"/> <input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}"> <div class="form-group"> <label>LastName</label> <input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}"> </div> <div class="form-group"> <label>Email</label> <input name="email" type="email" class="form-control" placeholder="zhangsan@atguigu.com" th:value="${emp!=null}?${emp.email}"> </div> <div class="form-group"> <label>Gender</label><br/> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}"> <label class="form-check-label"></label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}"> <label class="form-check-label"></label> </div> </div> <div class="form-group"> <label>department</label> <!--提交的是部门的id--> <select class="form-control" name="department.id"> <option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option> </select> </div> <div class="form-group"> <label>Birth</label> <input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"> </div> <button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button> </form> </main>

最后

以上就是贤惠冬天最近收集整理的关于Spring boot修改员工的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部