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

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

 更新時間:2022年05月28日 14:27:55   作者:把蘋果咬哭的測試筆記  
這篇文章主要為大家介紹了SpringMVC?RESTFul實戰(zhàn)案例修改功能實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

SpringMVC RESTFul實現(xiàn)修改功能

一、回顯功能

做實際修改操作之前,需要有個回顯功能,就是點編輯頁后可以看到數(shù)據(jù)。

1.修改操作超鏈接

這里的請求地址跟刪除的一樣,需要帶上 id,因為要回顯這個 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>

重新部署后,鼠標移動到更新按鈕上,瀏覽器左下角同樣可以顯示出請求的地址。

2.處理控制器方法

因為這個回顯操作請求,不僅僅是做視圖的返回,還要去獲取 id 下的信息,所以這里不能通過配置 view-controller 來實現(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,還有個形參 model,因為需要把查詢到的數(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>

因為需要回顯,所以還要加 value 的值,比如th:value="${employee.id}"。

另外,這里有 2 個隱藏域:

<input type="hidden" name="id" th:value="${employee.id}">,用來存放 id。

<input type="hidden" name="_method" value="put">,用于發(fā)送 put 請求。

重新部署測一下,點擊更新按鈕:

回顯成功。

二、修改功能

1.添加控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.PUT)
    public String updateEmployee(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/employee";
    }

調(diào)用 dao 里的 save() 方法,最后重定向到列表頁。

2.測試效果

重新部署后,點擊更新,修改3個數(shù)據(jù)測試下效果。

以上就是SpringMVC RESTFul實戰(zhàn)案例修改功能實現(xiàn)的詳細內(nèi)容,更多關于SpringMVC RESTFul修改的資料請關注腳本之家其它相關文章!

相關文章

最新評論