SpringMVC?RESTFul實(shí)戰(zhàn)案例修改功能實(shí)現(xiàn)
SpringMVC RESTFul實(shí)現(xiàn)修改功能
一、回顯功能
做實(shí)際修改操作之前,需要有個(gè)回顯功能,就是點(diǎn)編輯頁后可以看到數(shù)據(jù)。
1.修改操作超鏈接
這里的請(qǐng)求地址跟刪除的一樣,需要帶上 id,因?yàn)橐仫@這個(gè) id 的數(shù)據(jù)。
<td> <a @click="deleteEmployee" th:href="@{/employee/} + ${employee.id}" rel="external nofollow" >刪除</a> <a th:href="${/employee/} + ${employee.id}" rel="external nofollow" >更新</a> </td>
重新部署后,鼠標(biāo)移動(dòng)到更新按鈕上,瀏覽器左下角同樣可以顯示出請(qǐng)求的地址。
2.處理控制器方法
因?yàn)檫@個(gè)回顯操作請(qǐng)求,不僅僅是做視圖的返回,還要去獲取 id 下的信息,所以這里不能通過配置 view-controller 來實(shí)現(xiàn)了,需要編寫控制器方法。
繼續(xù)在類 EmployeeController 下新增方法:
@RequestMapping(value = "/employee/{id}", method = RequestMethod.GET) public String getEmployeeById(@PathVariable("id") Integer id, Model model) { Employee employee = employeeDao.get(id); model.addAttribute("employee", employee); return "employee_update"; }
這里除了 id,還有個(gè)形參 model,因?yàn)樾枰巡樵兊降臄?shù)據(jù)共享到 request 域中。最后返回修改頁。
3.創(chuàng)建修改頁面
新建 employee_update.html,可以拷貝新增頁的然后修改:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>更新員工</title> </head> <body> <form th:action="@{/employee}" method="post"> <input type="hidden" name="_method" value="put"> <input type="hidden" name="id" th:value="${employee.id}"> lastName:<input type="text" name="lastName" th:value="${employee.lastName}"><br> email:<input type="text" name="email" th:value="${employee.email}"><br> gender:<input type="radio" name="gender" value="1" th:field="${employee.gender}">male <input type="radio" name="gender" value="0" th:field="${employee.gender}">female<br> <input type="submit" value="更新"><br> </form> </body> </html>
因?yàn)樾枰仫@,所以還要加 value 的值,比如th:value="${employee.id}"。
另外,這里有 2 個(gè)隱藏域:
<input type="hidden" name="id" th:value="${employee.id}">,用來存放 id。
<input type="hidden" name="_method" value="put">,用于發(fā)送 put 請(qǐng)求。
重新部署測(cè)一下,點(diǎn)擊更新按鈕:
回顯成功。
二、修改功能
1.添加控制器方法
@RequestMapping(value = "/employee", method = RequestMethod.PUT) public String updateEmployee(Employee employee) { employeeDao.save(employee); return "redirect:/employee"; }
調(diào)用 dao 里的 save() 方法,最后重定向到列表頁。
2.測(cè)試效果
重新部署后,點(diǎn)擊更新,修改3個(gè)數(shù)據(jù)測(cè)試下效果。
以上就是SpringMVC RESTFul實(shí)戰(zhàn)案例修改功能實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC RESTFul修改的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringMVC?Restful風(fēng)格與中文亂碼問題解決方案介紹
- SpringMVC通過RESTful結(jié)構(gòu)實(shí)現(xiàn)頁面數(shù)據(jù)交互
- SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹
- SpringMVC?RESTFul實(shí)體類創(chuàng)建及環(huán)境搭建
- SpringMVC?RESTFul實(shí)戰(zhàn)案例訪問首頁
- SpringMVC?RESTFul實(shí)現(xiàn)列表功能
- SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn)
- 關(guān)于SpringMVC對(duì)Restful風(fēng)格的支持詳解
- SpringMVC使用RESTful接口案例詳解
相關(guān)文章
SpringBoot2.7?WebSecurityConfigurerAdapter類過期配置
這篇文章主要為大家介紹了SpringBoot2.7中WebSecurityConfigurerAdapter類過期應(yīng)該如何配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06mybatis?實(shí)體類字段大小寫問題?字段獲取不到值的解決
這篇文章主要介紹了mybatis?實(shí)體類字段大小寫問題?字段獲取不到值的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis Generator自動(dòng)生成對(duì)應(yīng)文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis Generator自動(dòng)生成對(duì)應(yīng)的文件的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09Springboot 使用內(nèi)置tomcat禁止不安全HTTP的方法
這篇文章主要介紹了Springboot 使用內(nèi)置tomcat禁止不安全HTTP的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java異常詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
異常是Java語言中的一部分,它代表程序中由各種原因引起的“不正?!币蛩?。下面通過本文給大家介紹java異常的相關(guān)知識(shí),感興趣的朋友一起看看吧2017-06-06Windows7下的Java運(yùn)行環(huán)境搭建過程圖解
這篇文章主要介紹了Windows7下的Java運(yùn)行環(huán)境搭建過程圖解,需要的朋友可以參考下2014-04-04詳解Servlet入門級(jí)設(shè)置(超詳細(xì) IDEA2020版)
這篇文章主要介紹了詳解Servlet入門級(jí)設(shè)置(超詳細(xì) IDEA2020版),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11