日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring boot修改员工

發布時間:2025/3/20 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring boot修改员工 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

修改員工

點擊修改按鈕,根據用戶id
查詢用戶信息,查詢所有的部門列表信息

回顯到修改頁面
點擊確認,提交用戶信息

用戶列表頁面

<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,查詢用戶信息
查詢所有部門信息,返回修改頁面

//來到修改頁面,查出當前員工,在頁面回顯 @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"; }

回顯用戶信息

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

回顯選中部門
如果,當前部門id,等于用戶的部門id

th:selected

設置為選中

<!--提交的是部門的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

格式化為指定日期格式

<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不為空,顯示用戶信息

th:checked="${emp!=null}?${emp.gender==1}

提交按鈕
添加的時候,顯示添加
修改的時候,顯示修改

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

點擊添加
設置發送put請求

還是會執行form表單的action請求
提交方式,使用配置的put方式提交

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

Controller

保存員工信息

//員工修改;需要提交員工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

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

公共頁面

<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修改员工的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。