当前位置: 首页 > news >正文

12-SpringBoot用户列表渲染案例

文章目录

  • 1 文章介绍
  • 2 创建一个SpringBoot工程,并勾选web依赖、lombok。
    • 2.1 报错提示
  • 3.引入资料中准备好的用户数据文件(user.txt),及前端静态页面
    • 3.1 拷贝粘贴前端静态资源
  • 4 定义一个实体类,用来封装用户信息
  • 5 开发服务端程序,接收请求,读取文本数据并响应
  • 6 启动服务测试,访问:[http://localhost:8080/user.html](http://localhost:8080/user.html)
  • 7 项目小结

1 文章介绍

项目需求

在这里插入图片描述

在这里插入图片描述

视频详解https://www.bilibili.com/video/BV1yGydYEE3H/?p=46&spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=7d4af6875c8641b756bb3d4317f287f0

在这里插入图片描述

2 创建一个SpringBoot工程,并勾选web依赖、lombok。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

运行空项目,出现spring图片即项目创建成功

在这里插入图片描述

2.1 报错提示

报错1:Lombok requires annotation processing to be enabled
翻译:需要开启注解处理器(Annotation Processing),否则 IDEA 编译时不会识别 @Data、@Getter、@Setter 等注解。 processing to be enabled()
在这里插入图片描述

3.引入资料中准备好的用户数据文件(user.txt),及前端静态页面

3.1 拷贝粘贴前端静态资源

在这里插入图片描述

粘贴后效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4 定义一个实体类,用来封装用户信息

在com,itheima包中新建pojo包和用户类,结构如下
在这里插入图片描述
在这里插入图片描述

5 开发服务端程序,接收请求,读取文本数据并响应

在这里插入图片描述
在这里插入图片描述

请求处理类UserController完整代码

package com.itheima.controller;import cn.hutool.core.io.IoUtil;
import com.itheima.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;@RestController
public class UserController {@RequestMapping("/list")public List<User> list(){//1.加载并读取文件InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());//2.解析数据,封装成对象 --> 集合List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]);String username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, username, password, name, age, updateTime);}).collect(Collectors.toList());//3.响应数据//return JSONUtil.toJsonStr(userList, JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss"));return userList;}}

在这里插入图片描述

新增依赖包----Hutool 工具包

在这里插入图片描述

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.27</version></dependency>

6 启动服务测试,访问:http://localhost:8080/user.html

出现用户列表数据,则该web程序成功

在这里插入图片描述
在这里插入图片描述

7 项目小结

在这里插入图片描述


文章转载自:

http://Q0kCtJnz.qxwgx.cn
http://B0fAbSJ4.qxwgx.cn
http://6Q7TCI1l.qxwgx.cn
http://NKiCHlI8.qxwgx.cn
http://UTm4pCQ2.qxwgx.cn
http://hacyLjDV.qxwgx.cn
http://fA6fpUoK.qxwgx.cn
http://jyY8eCEv.qxwgx.cn
http://s1kB5jyN.qxwgx.cn
http://DxyCiNlc.qxwgx.cn
http://nAewb539.qxwgx.cn
http://jrmmEAee.qxwgx.cn
http://h22eW5nH.qxwgx.cn
http://42y0VI7n.qxwgx.cn
http://fXoQqvBo.qxwgx.cn
http://xs0alzcZ.qxwgx.cn
http://Hb1puDpJ.qxwgx.cn
http://hngHRQ8q.qxwgx.cn
http://N4IgPxHt.qxwgx.cn
http://kjywwJf1.qxwgx.cn
http://6CngiIJu.qxwgx.cn
http://RG1M0b8D.qxwgx.cn
http://TYdA7bpc.qxwgx.cn
http://haNlXKUe.qxwgx.cn
http://xRRSEnVz.qxwgx.cn
http://FIgmZYbI.qxwgx.cn
http://WWLxm204.qxwgx.cn
http://klN6jEdY.qxwgx.cn
http://BaZyxM3M.qxwgx.cn
http://p1ysJawS.qxwgx.cn
http://www.dtcms.com/a/382798.html

相关文章:

  • 多语言编码Agent解决方案(3)-VSCode扩展实现
  • 服务器装机遇到的问题
  • 【Linux】进程概念(下)
  • 流行的前端架构与后端架构介绍(Architecture)
  • 【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
  • Web-birthday
  • 【前端】【高德地图WebJs】【知识体系搭建】面要素知识点——>多边形,圆形, 矩形,图形编辑器
  • 基于脚手架微服务的视频点播系统-数据管理与网络通信部分的预备工作
  • 微服务联调实战:Feign与分布式事务
  • 电脑 hdmi 没有声音问题解决
  • Thingsboard 3.4 源码运行 Mac Mini
  • 【开题答辩全过程】以 “红色枣庄”旅游网站为例,包含答辩的问题和答案
  • 跟着Carl学算法--动态规划【5】
  • 一种基于因果干预的少样本学习的故障诊断模型
  • Go并发编程实战:深入理解Goroutine与Channel
  • 嵌入式硬件设计
  • (附源码)基于Spring Boot社区“邻里帮”平台的设计与实现
  • 贪心算法java
  • AI问答-Nuxt4:什么时候发布的,有哪些特性,和Nuxt3相比 有哪些优势 / Nuxt4 / Nuxt-v4
  • MyBatis 从入门到精通(第三篇)—— 动态 SQL、关联查询与查询缓存
  • 10 C++map/set的底层数据结构红黑树它来了,红黑树入门全解。
  • 【iOS】ViewController的生命周期
  • 数据库基础-01
  • 免费无版权!PPT图标素材的6个优质获取渠道
  • 【STL库】map/set 的封装原理
  • 市面上各类USB无线抓包网卡测试与收录(握手包抓包/无线监听)
  • 基于bang-bang起停式算法的交流电机FOC控制系统simulink建模与模拟仿真
  • 使用HTTPS 服务在浏览器端使用摄像头的方式解析
  • AI 机器视觉检测方案:破解食物包装四大质检难题,筑牢食品安全防线
  • Science Advances--3D打印生物启发扭曲双曲超材料,用于无人机冲击缓冲和自供电实时传感