SpringMVC RESTFul实现列表功能
作者:把苹果咬哭的测试笔记 时间:2023-10-30 21:15:22
SpringMVC RESTFul列表功能实现
一、增加控制器方法
在控制器类 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 层,也就是调用 employeeDao.getAll()来获取所有员工信息,返回是一个列表集合。
接着把数据放到 request 域里,供前端页面使用,这里使用前面讲过的 Model 方法。
在model.addAttribute("employeeList", employeeList); 中,2个分别对应 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>
<!--循环后端放到request域中的数据 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>
这里使用了简单的样式,使其看起来更像个列表。
每一行的数据,要通过循环后端放到 request 域中的数据 employeeList,得到单个对象 employee,然后就可以将对象的属性获取出来展示, 比如 employee.id 。
th:each,${}这些都是 thymeleaf 的用法。
三、访问列表页
重新部署应用。
因为在首页中,已经加了跳转到列表页的超链接,直接点击。
访问成功,忽略掉好不好看的问题,起码这是一个正常的列表。
感谢《尚硅谷》的学习资源,更多关于SpringMVC RESTFul列表的资料请关注脚本之家其它相关文章!
来源:https://blog.csdn.net/wessonlan/article/details/124812965
标签:SpringMVC,RESTFul,列表
0
投稿
猜你喜欢
flutter BottomAppBar实现不规则底部导航栏
2023-06-19 23:40:19
Java通过FTP服务器上传下载文件的方法
2021-08-15 07:26:39
使用JPA自定义VO类型转换(EntityUtils工具类)
2023-08-26 14:56:17
Android ViewPager小圆点指示器
2022-05-09 11:21:43
Java file类中renameTo方法操作实例
2021-06-13 01:21:03
关于C++运算符重载的一些困惑详解
2023-05-25 15:33:20
Spring Retry 重试实例详解
2021-07-15 22:43:26
C#遍历DataSet控件实例总结
2022-11-13 15:22:36
Java调用groovy脚本的方式分享
2022-09-25 09:20:24
Java中LinkedList和ArrayList的效率分析
2023-02-12 06:21:55
Mybatis一对一延迟加载实现过程解析
2022-09-07 12:45:43
Android Drawable必备知识小结
2021-09-09 20:56:16
浅谈EventBus
2022-12-31 20:23:14
java IO流 之 输出流 OutputString()的使用
2023-08-11 23:16:30
浅谈Android串口通讯SerialPort原理
2023-03-31 12:39:52
Android自定义View绘制的方法及过程(二)
2023-05-02 14:42:17
Android开发之搜索框SearchView用法示例
2021-10-30 03:40:19
C#算法之各位相加
2021-09-03 17:32:42
Android基于广播事件机制实现简单定时提醒功能代码
2023-07-10 09:57:55
Java 高并发五:JDK并发包1详细介绍
2022-11-29 08:56:26