SpringMVC RESTFul实现列表功能

2023-01-21 0 2,686

目录

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实现列表功能

因为在首页中,已经加了跳转到列表页的超链接,直接点击。

SpringMVC RESTFul实现列表功能

访问成功,忽略掉好不好看的问题,起码这是一个正常的列表。

感谢《尚硅谷》的学习资源,更多关于SpringMVC RESTFul列表的资料请关注其它相关文章!

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 SpringMVC RESTFul实现列表功能 http://www.20zxx.cn/463279/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务