SpringMVC?RESTFul實(shí)現(xiàn)列表功能
SpringMVC RESTFul列表功能實(shí)現(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)用。

因?yàn)樵谑醉撝校呀?jīng)加了跳轉(zhuǎn)到列表頁的超鏈接,直接點(diǎn)擊。

訪問成功,忽略掉好不好看的問題,起碼這是一個正常的列表。
感謝《尚硅谷》的學(xué)習(xí)資源,更多關(guān)于SpringMVC RESTFul列表的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringMVC使用RESTful接口案例詳解
- 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í)戰(zhàn)案例刪除功能實(shí)現(xiàn)
- SpringMVC使用RESTful接口案例
相關(guān)文章
SpringBoot?docker項(xiàng)目部署實(shí)戰(zhàn)
本文主要介紹了SpringBoot?docker項(xiàng)目部署實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
Spring?Boot+RabbitMQ?通過fanout模式實(shí)現(xiàn)消息接收功能(支持消費(fèi)者多實(shí)例部署)
這篇文章主要介紹了Spring?Boot+RabbitMQ?通過fanout模式實(shí)現(xiàn)消息接收(支持消費(fèi)者多實(shí)例部署),本文通過案例場景分析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
java字符串轉(zhuǎn)數(shù)字及各種數(shù)字轉(zhuǎn)字符串的3種方法
這篇文章主要介紹了java字符串轉(zhuǎn)數(shù)字及各種數(shù)字轉(zhuǎn)字符串的3種方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
Spring?Boot?實(shí)現(xiàn)?WebSocket?的代碼示例
WebSocket?協(xié)議是獨(dú)立的基于?TCP?協(xié)議。它與?HTTP?的唯一關(guān)系是,它的握手會被?HTTP?服務(wù)器解釋為?Upgrade?請求,接下來通過本文給大家介紹Spring?Boot?實(shí)現(xiàn)?WebSocket?示例詳解,需要的朋友可以參考下2022-04-04
Java特性?Lambda?表達(dá)式和函數(shù)式接口
這篇文章主要介紹了Java特性?Lambda?表達(dá)式和函數(shù)式接口,Lambda表達(dá)式基于函數(shù)式編程思想,也可以稱為閉包,是Java?8引入的重要新特性,?Lambda允許把函數(shù)作為一個方法的參數(shù)2022-06-06
SpringBoot整合TKMyBatis實(shí)現(xiàn)單表增刪改查操作
據(jù)說tk.mybatis能夠讓我不寫sql代碼就可以所有單表操作問題,作為熱愛偷懶的我,怎么能放過這種機(jī)會。talk is cheap, show me the code。趕緊搞個例子爽一把先2023-01-01

