SpringMVC?RESTFul實現(xiàn)列表功能
SpringMVC RESTFul列表功能實現(xiàn)
一、增加控制器方法
在控制器類 EmployeeController 中,添加訪問列表方法。
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public String getAllEmployee(Model model) {
Collection<Employee> employeeList = employeeDao.getAll();
model.addAttribute("employeeList", employeeList);
return "employee_list";
}
}- 這里就沒寫 service 層了,直接在 getAllEmployee() 方法中操作 dao 層,也就是調(diào)用 employeeDao.getAll()來獲取所有員工信息,返回是一個列表集合。
- 接著把數(shù)據(jù)放到 request 域里,供前端頁面使用,這里使用前面講過的 Model 方法。
- 在model.addAttribute("employeeList", employeeList); 中,2個分別對應(yīng) key - value,頁面里使用 key 可以獲取到 value 。
- 最后返回 employee_list 頁面。
二、編寫列表頁 employee_list.html
控制器里返回了 employee_list ,這是一個 html 頁面,依然寫在 templates 下面:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>員工信息</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0" style="text-align: center;">
<tr>
<th colspan="5">員工列表</th>
</tr>
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>options</th>
</tr>
<!--循環(huán)后端放到request域中的數(shù)據(jù) employeeList-->
<tr th:each="employee : ${employeeList}">
<td th:text="${employee.id}"></td>
<td th:text="${employee.lastName}"></td>
<td th:text="${employee.email}"></td>
<td th:text="${employee.gender}"></td>
<td>
<a href="">刪除</a>
<a href="">更新</a>
</td>
</tr>
</table>
</body>
</html>- 這里使用了簡單的樣式,使其看起來更像個列表。
- 每一行的數(shù)據(jù),要通過循環(huán)后端放到 request 域中的數(shù)據(jù) employeeList,得到單個對象 employee,然后就可以將對象的屬性獲取出來展示, 比如 employee.id 。
- th:each,${}這些都是 thymeleaf 的用法。
三、訪問列表頁
重新部署應(yīng)用。

因為在首頁中,已經(jīng)加了跳轉(zhuǎn)到列表頁的超鏈接,直接點擊。

訪問成功,忽略掉好不好看的問題,起碼這是一個正常的列表。
感謝《尚硅谷》的學(xué)習(xí)資源,更多關(guān)于SpringMVC RESTFul列表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java動態(tài)規(guī)劃算法——硬幣找零問題實例分析
這篇文章主要介紹了java動態(tài)規(guī)劃算法——硬幣找零問題,結(jié)合實例形式分析了java動態(tài)規(guī)劃算法——硬幣找零問題相關(guān)原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-05-05
解決springboot的findOne方法沒有合適的參數(shù)使用問題
這篇文章主要介紹了解決springboot的findOne方法沒有合適的參數(shù)使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringMVC?bean實現(xiàn)加載控制方法詳解
SpringMVC是一種基于Java,實現(xiàn)了Web?MVC設(shè)計模式,請求驅(qū)動類型的輕量級Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡埱篁?qū)動指的就是使用請求-響應(yīng)模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們?nèi)粘eb開發(fā)2022-08-08
Java數(shù)據(jù)結(jié)構(gòu)及算法實例:冒泡排序 Bubble Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:冒泡排序 Bubble Sort,本文直接給出實現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06

