深圳做棋牌網(wǎng)站建設(shè)哪家便宜線上推廣方式都有哪些
注意:spring boot專欄是一個新手項目,博文順序則是功能實現(xiàn)的流程,如果有看不懂的內(nèi)容可以到前面系列去了解。
本次項目所有能夠使用的靜態(tài)資源可以免費進(jìn)行下載
靜態(tài)資源
在本篇代碼DAO層將通過Java文件去實現(xiàn),在這里就不連接數(shù)據(jù),然后通過jdbc將數(shù)據(jù)庫內(nèi)容的內(nèi)容顯示出來
案例:員工管理系統(tǒng)
上一篇博客主要的講解了,如何添加進(jìn)行添加員工的信息,這一篇文章實現(xiàn)的是,修改現(xiàn)有的員工信息
在展示員工信息的頁面添加一個按鈕,將要修改的員工id傳遞給后端指定域名中
在展示員工信息有這樣的一行代碼,實現(xiàn)的功能是點擊進(jìn)行網(wǎng)頁跳轉(zhuǎn),并帶著id值
?網(wǎng)頁展示
點擊編輯按鈕,則將帶著指定的id值去訪問指定域名
@RequestMapping("/update/{id}")public String updateData(@PathVariable("id") Integer id,Model model){//獲取前端所傳來的數(shù)據(jù)idEmployee getemployeeid = employeeDAO.getemployeeid(id); //在DAO層創(chuàng)建指定查詢方法,查詢指定id的用戶信息 model.addAttribute("emp",getemployeeid); //將查詢來的數(shù)據(jù),添加到Model中Collection<department> getall = departmentDAO.getdepartments(); //department名字model.addAttribute("departs",getall);return "update";}
?前端會將id值傳遞到這個方法中,在這個方法實現(xiàn)查詢數(shù)據(jù),然后將得到得數(shù)據(jù)封裝到Model中,傳遞給指定網(wǎng)頁中,這里使用得update網(wǎng)頁則是在添加用戶信息那個網(wǎng)頁的基礎(chǔ)上進(jìn)行更改的
更改代碼一:
?添加這一行代碼實現(xiàn)的功能:修改指定id對應(yīng)的信息,并非在原有的基礎(chǔ)上進(jìn)行添加,如果沒有這一行代碼,你會發(fā)現(xiàn),編輯提交時原本的數(shù)據(jù)并沒有發(fā)生改變,只是在原有的基礎(chǔ)又添加了一行
例如:
這是原始數(shù)據(jù)
?實現(xiàn)將id=1001的lastname 改為FF
?你會發(fā)現(xiàn)AA的姓名沒有發(fā)生改變,但是卻又添加了一行新的內(nèi)容FF
這里出現(xiàn)的問題就是修改員工的內(nèi)容,并沒有在指定的id為多少時,進(jìn)行該操作,上面那一行代碼則是實現(xiàn)在指定id值為多少時,修改其內(nèi)容。
修改代碼二:
<form class="am-form am-form-horizontal" th:action="@{/updateEmp}" method="post">
<!-- <input name="id" type="hidden" th:value="${emp.getId()}">--><div class="am-form-group"><label class="am-u-sm-3 am-form-label">姓名</label><div class="am-u-sm-9"><input required="" placeholder="請輸入姓名" name="lastname" type="text" th:value="${emp.getLastname()}"><small>請輸入姓名。</small></div></div><div class="am-form-group"><label class="am-u-sm-3 am-form-label">郵箱</label><div class="am-u-sm-9"><input required="" placeholder="請輸入郵箱" name="Email" type="text" th:value="${emp.getEmail()}"><small >請輸入郵箱。</small></div></div><div class="am-form-group"><label class="am-u-sm-3 am-form-label">性別 / sex</label><div class="am-u-sm-9" style="line-height: 30px;"><input type="radio" id="man" name="gender" value="1" th:checked="${emp.getGender()==1}"/><label for="man">男</label> <input type="radio" id="woman" name="gender" value="0" th:checked="${emp.getGender()==0}" /><label for="woman">女</label><br /><small>請選擇性別</small></div></div><div class="am-form-group"><label class="am-u-sm-3 am-form-label">部門</label><div class="am-u-sm-9"><select class="form-control" name="department.id"><option th:selected="${dept.getId()==emp.getDepartment().getId()}" th:each="dept:${departs}" th:text="${dept.getDepartmentname()}" th:value="${dept.getId()}"></option></select><small>請選擇部門</small></div></div><div class="am-form-group"><label class="am-u-sm-3 am-form-label">生日</label><div class="am-u-sm-9">
<!-- <input required="" placeholder="請輸入生日" th:value="${#dates.format(emp.getDate(),'yyyy-MM-dd')}" name="date" type="text">--><input required="" placeholder="請輸入生日" th:value="${emp.getDate()}" name="date" type="text"><small>請輸入生日。</small></div></div><div class="am-form-group"><div class="am-u-sm-9 am-u-sm-push-3"><input class="am-btn am-btn-success" value="修改" type="submit"></div></div></form>
代碼實現(xiàn)則是將后端傳遞的數(shù)據(jù)顯示在指定的input標(biāo)簽中。在每個屬性中,添加一個value屬性,屬性值為后端剛剛傳遞過來的emp中employee的各個屬性值,這是一個表單,修改完成之后,則是將表單進(jìn)行提交
在控制其中添加一個方法,接收表單中的內(nèi)容
@PostMapping("/updateEmp")public String updaterr(Employee employee){ //接收前端表單中所傳遞的數(shù)據(jù),并封裝在Employee類中employeeDAO.save(employee); //調(diào)用方法,將數(shù)據(jù)在數(shù)組中進(jìn)行修改return "redirect:/emps"; //網(wǎng)頁跳轉(zhuǎn),到顯示員工信息的域名}
代碼部分完成
運行項目:
還是將id=1001的lastname修改成FF
?代碼修改成功
?