欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC?RESTFul實(shí)戰(zhàn)案例修改功能實(shí)現(xiàn)

 更新時(shí)間:2022年05月28日 14:27:55   作者:把蘋果咬哭的測(cè)試筆記  
這篇文章主要為大家介紹了SpringMVC?RESTFul實(shí)戰(zhàn)案例修改功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jì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)文章!

相關(guān)文章

最新評(píng)論