13、SpringBoot-CRUD員工修改操作/刪除
阿新 • • 發佈:2019-02-09
一個 mit zhang 添加 ast pin EDA spa 並且
對於修改連接的uri
在list.html中
<a class="btn btn-sm btn-primary" th:href="@{/update/} + ${emp.id} ">修改</a>修改需要知道id,所以路徑上需要有有該修改的員工id 兩個屬性是要進行拼串的不可以寫在一起
controller實現頁面的跳轉
//修改 @GetMapping("/update/{id}") public String updataEmp(@PathVariable("id")Integer id, Model model){ Employee employee= employeeDao.get(id); model.addAttribute("emp",employee); //部門選擇的修改 Collection<Department> departments = departmentDao.getDepartments(); model.addAttribute("depts",departments); return "emp/update"; }
在add.html文件夾中復制並且命名為update.html 註意使用的RESTFUL方式,使用put請求時的隱藏域以及id的隱藏域 對數據進行的回顯判斷操作
<form th:action="@{/update1}"method="post"> <!--發送put請求修改員工數據--> <!-- 1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自動配置好的) 2、頁面創建一個post表單 3、創建一個input項,name="_method";值就是我們指定的請求方式 --> <input type="hidden" name="_method" value="put" th:if="${emp!=null}"/> <!-- id --> <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="[email protected]" 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:each="dept:${depts}" th:text="${dept.departmentName}" th:selected="${dept.id == emp.department.id}" th:value="${dept.id}">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>
實現頁面修改的controller:
//員工修改 @PutMapping("/update1") public String updataToEmp(Employee employee){ System.out.println(employee); //修改的數據 employeeDao.save(employee); return "redirect:/emps"; }此時可以成功添加數據 修改數據
在刪除頁面的標誌:
list.html中
<form th:action="@{/delete/}+${emp.id}" method="post"> <input type="hidden" name="_method" value="delete"> <button class="btn btn-sm btn-danger">刪除</button> </form>
controller實現:
//刪除請求 @DeleteMapping("/delete/{id}") public String delete(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/emps"; }
13、SpringBoot-CRUD員工修改操作/刪除