SpringBoot学习笔记-创建个人中心页面(下)
笔记内容转载自 AcWing 的 SpringBoot 框架课讲义,课程链接:AcWing SpringBoot 框架课。
CONTENTS
- 1. 实现个人中心页面
- 2. POJO时区修改
- 3. 集成代码编辑器
本节实现个人中心的前端页面,用户能够查看自己的 Bot 信息,并能创建、修改或删除 Bot,此外还集成了 Vue Ace Editor 代码编辑器,方便用户输入 Bot 的代码。
1. 实现个人中心页面
我们要实现用户的个人中心页面,能够展示用户的头像以及自己所有 Bot 的信息,例如 Bot 名字、创建时间等,还需要具有创建/修改/删除 Bot 等功能,在很早之前创建的 MyBotsIndexView 组件中实现:
<template><div class="container"><div class="card text-bg-secondary" style="margin-top: 20px;"><div class="card-header"><h3>My Bots</h3></div><div class="card-body" style="background-color: rgba(255, 255, 255, 0.5);"><div class="row"><div class="col-md-3"><div class="card"><!-- 用户头像与用户名 --><div class="card-body text-center"><img class="img-fluid" :src="$store.state.user.photo" style="width:50%; border-radius: 50%;"><div style="font-size: 20px; margin-top: 10px; font-weight: bold;">{{ $store.state.user.username }}</div></div></div></div><div class="col-md-9"><div class="card"><div class="card-header"><span style="font-size: 25px;">Bot 管理</span><!-- 点击创建按钮能够打开模态框 --><button class="btn btn-outline-success float-end" type="button" data-bs-toggle="modal" data-bs-target="#add_bot_modal">创建 Bot</button><!-- 创建 Bot 的模态框 --><div class="modal fade" id="add_bot_modal" tabindex="-1"><div class="modal-dialog modal-xl"><div class="modal-content"><div class="modal-header" style="background-color: #198754;"><h1 class="modal-title fs-5" style="color: white;">创建 Bot</h1><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><div class="mb-3"><label for="title" class="form-label">名称</label><input v-model="new_bot.title" type="email" class="form-control" id="title" placeholder="请输入 Bot 名称"></div><div class="mb-3"><label for="description" class="form-label">简介</label><textarea v-model="new_bot.description" class="form-control" id="description" rows="3" placeholder="介绍一下你的 Bot 吧~(可以暂时不填)"></textarea></div><div class="mb-3"><label for="content" class="form-label">代码</label><textarea v-model="new_bot.content" class="form-control" id="content" rows="14" placeholder="在这里编写你的 Bot 代码~"></textarea></div></div><div class="modal-footer"><div class="error_message" style="color: red;">{{ new_bot.error_message }}</div><button type="button" class="btn btn-success" @click="add_bot">创建</button><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button></div></div></div></div></div><!-- 用户的 Bot 列表 --><div class="card-body"><table class="table table-striped table-hover"><thead><tr><th>名称</th><th>创建时间</th><th>操作</th></tr></thead><tbody><tr v-for="bot in bots" :key="bot.id"><td style="line-height: 32px;">{{ bot.title }}</td><td style="line-height: 32px;">{{ bot.createtime }}</td><td><button class="btn btn-outline-primary" type="button" data-bs-toggle="modal" :data-bs-target="'#update_bot_modal_' + bot.id" style="margin-right: 10px; padding: 3px 10px;">修改</button><button class="btn btn-outline-danger" type="button" data-bs-toggle="modal" :data-bs-target="'#remove_bot_modal_' + bot.id" style="padding: 3px 10px;">删除</button><!-- 修改 Bot 的模态框 --><div class="modal fade" :id="'update_bot_modal_' + bot.id" tabindex="-1"><div class="modal-dialog modal-xl"><div class="modal-content"><div class="modal-header" style="background-color: #0D6EFD;"><h1 class="modal-title fs-5" style="color: white;">修改 Bot</h1><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><div class="mb-3"><label for="title" class="form-label">名称</label><input v-model="bot.title" type="email" class="form-control" id="title" placeholder="请输入 Bot 名称"></div><div class="mb-3"><label for="description" class="form-label">简介</label><textarea v-model="bot.description" class="form-control" id="description" rows="3" placeholder="介绍一下你的 Bot 吧~(可以暂时不填)"></textarea></div><div class="mb-3"><label for="content" class="form-label">代码</label><textarea v-model="bot.content" class="form-control" id="content" rows="14" placeholder="在这里编写你的 Bot 代码~"></textarea></div></div><div class="modal-footer"><div class="error_message" style="color: red;">{{ new_bot.error_message }}</div><button type="button" class="btn btn-primary" @click="update_bot(bot)">保存修改</button><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button></div></div></div></div><!-- 删除 Bot 的确认模态框 --><div class="modal fade" :id="'remove_bot_modal_' + bot.id" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content"><div class="modal-header" style="background-color: #DC3545;"><h1 class="modal-title fs-5" style="color: white;">警告</h1><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body" style="font-size: 20px;">确认删除?该操作无法撤销哦~</div><div class="modal-footer"><button type="button" class="btn btn-danger" @click="remove_bot(bot)">删除</button><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button></div></div></div></div></td></tr></tbody></table></div></div></div></div></div></div></div>
</template><script>
import { ref, reactive } from "vue"; // reactive用来让组件绑定对象
import { useStore } from "vuex";
import { Modal } from "bootstrap/dist/js/bootstrap";
import $ from "jquery";export default {setup() {const store = useStore();let bots = ref([]); // bot列表const new_bot = reactive({ // 要新创建的bot对象title: "",description: "",content: "",error_message: "",});const get_bots = () => {$.ajax({url: "http://localhost:3000/user/bot/getlist/",type: "GET",headers: {Authorization: "Bearer " + store.state.user.jwt_token,},success(resp) {bots.value = resp; // 后端返回的就是一个列表},});};get_bots(); // 定义完后直接执行一遍const add_bot = () => {new_bot.error_message = "";$.ajax({url: "http://localhost:3000/user/bot/add/",type: "POST",data: {title: new_bot.title,description: new_bot.description,content: new_bot.content,},headers: {Authorization: "Bearer " + store.state.user.jwt_token,},success(resp) {if (resp.result === "success") {// 需要清空信息防止下次打开创建模态框时还留有上次的信息new_bot.title = "";new_bot.description = "";new_bot.content = "";Modal.getInstance("#add_bot_modal").hide(); // 关闭模态框get_bots(); // 刷新一下Bot列表} else {new_bot.error_message = resp.result;}},});};const remove_bot = (bot) => {$.ajax({url: "http://localhost:3000/user/bot/remove/",type: "POST",data: {bot_id: bot.id,},headers: {Authorization: "Bearer " + store.state.user.jwt_token,},success(resp) {if (resp.result === "success") {Modal.getInstance("#remove_bot_modal_" + bot.id).hide();get_bots();}},});};const update_bot = (bot) => {new_bot.error_message = "";$.ajax({url: "http://localhost:3000/user/bot/update/",type: "POST",data: {bot_id: bot.id,title: bot.title,description: bot.description,content: bot.content,},headers: {Authorization: "Bearer " + store.state.user.jwt_token,},success(resp) {if (resp.result === "success") {Modal.getInstance("#update_bot_modal_" + bot.id).hide();get_bots();} else {new_bot.error_message = resp.result;}},});};return {bots,new_bot,add_bot,remove_bot,update_bot,};},
};
</script><style scoped></style>
注意,我们实现了当点击创建 Bot 按钮时弹出一个模态框(Bootstrap 中的 Modal)的效果,然后用户可以在里面编辑 Bot 的信息。但对于每个 Bot 的修改和删除应该都会分别对应一个模态框,而不像创建那样只有一个模态框,因此每个模态框还需要加一个 ID 来对应。
2. POJO时区修改
现在我们会发现前端页面中显示的 Bot 创建时间和后端数据库中的不一致,需要在 pojo 中的 Bot 类中修改:
package com.kob.backend.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Date;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bot {@TableId(value = "id", type = IdType.AUTO) // 声明id为自增类型private Integer id;private Integer userId; // 注意驼峰命名,userId之后会被解析为user_id,别写成userID,因为这样会解析成user_i_dprivate String title;private String description;private String content;private Integer rating;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") // 注意日期格式和时区的设置private Date createtime;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")private Date modifytime;
}
3. 集成代码编辑器
由于 Bot 的内容有代码部分,之前我们用的是 <textarea> 让用户编写代码,这样编写体验不好,因此我们需要集成一个代码编辑器,我们使用的是 Vue Ace Editor(Ace GitHub 官网:Ace (Ajax.org Cloud9 Editor),Vue 3 版 Ace GitHub 官网:vue3-ace-editor)。
先在 Vue 的控制台中安装以下依赖:
vue3-ace-editorace-builds
然后就可以用 <VAceEditor> 替代之前的 <textarea>:
<template><div class="container"><div class="card text-bg-secondary" style="margin-top: 20px;">...<div class="card-body" style="background-color: rgba(255, 255, 255, 0.5);"><div class="row">...<div class="col-md-9"><div class="card"><div class="card-header">...<!-- 创建 Bot 的模态框 --><div class="modal fade" id="add_bot_modal" tabindex="-1"><div class="modal-dialog modal-xl"><div class="modal-content">...<div class="modal-body">...<div class="mb-3"><label for="content" class="form-label">代码</label><VAceEditorv-model:value="new_bot.content"@init="editorInit"lang="c_cpp"theme="textmate"style="height: 400px":options="{enableBasicAutocompletion: true, //启用基本自动完成enableSnippets: true, // 启用代码段enableLiveAutocompletion: true, // 启用实时自动完成fontSize: 16, //设置字号tabSize: 4, // 标签大小showPrintMargin: false, //去除编辑器里的竖线highlightActiveLine: true, // 选中行高亮显示}"/></div></div>...</div></div></div></div><!-- 用户的 Bot 列表 --><div class="card-body"><table class="table table-striped table-hover">...<tbody><tr v-for="bot in bots" :key="bot.id"><td style="line-height: 32px;">{{ bot.title }}</td><td style="line-height: 32px;">{{ bot.createtime }}</td><td>...<!-- 修改 Bot 的模态框 --><div class="modal fade" :id="'update_bot_modal_' + bot.id" tabindex="-1"><div class="modal-dialog modal-xl"><div class="modal-content">...<div class="modal-body">...<div class="mb-3"><label for="content" class="form-label">代码</label><VAceEditorv-model:value="bot.content"@init="editorInit"lang="c_cpp"theme="textmate"style="height: 400px":options="{enableBasicAutocompletion: true,enableSnippets: true,enableLiveAutocompletion: true,fontSize: 16,tabSize: 4,showPrintMargin: false,highlightActiveLine: true,}"/></div></div>...</div></div></div><!-- 删除 Bot 的确认模态框 -->...</td></tr></tbody></table></div></div></div></div></div></div></div>
</template><script>
...
import { VAceEditor } from "vue3-ace-editor";
import ace from "ace-builds";
import "ace-builds/src-noconflict/mode-json";
import "ace-builds/src-noconflict/theme-chrome";
import "ace-builds/src-noconflict/ext-language_tools";
import "ace-builds/src-noconflict/mode-c_cpp";export default {components: {VAceEditor,},setup() {ace.config.set("basePath","https://cdn.jsdelivr.net/npm/ace-builds@" +require("ace-builds").version +"/src-noconflict/");...},
};
</script><style scoped></style>
相关文章:
SpringBoot学习笔记-创建个人中心页面(下)
笔记内容转载自 AcWing 的 SpringBoot 框架课讲义,课程链接:AcWing SpringBoot 框架课。 CONTENTS 1. 实现个人中心页面2. POJO时区修改3. 集成代码编辑器 本节实现个人中心的前端页面,用户能够查看自己的 Bot 信息,并能创建、修改…...
电子秤方案:做一个宠物勺方案设计
养宠物是一件费心劳力的事情,但同时也是能够给你带来快乐和幸福感的事情。就是有时候会怕宠物毫无征兆地生病令人措手不及,所以电子秤方案设计鼎盛合科技分享一个小方案,能够及时了解到宠物的身体状况问题。 蓝牙宠物勺是一种具有记录和称重…...
Debezium-Embedded 实时监控MySQL数据变更
1.Debezium-Embedded 简介 Debezium连接器的操作通常是将它们部署到Kafka Connect服务,并配置一个或多个连接器来监控上游数据库,并为它们在上游数据库中看到的所有更改生成数据更改事件。这些数据更改事件被写入Kafka,在那里它们可以被许多不…...
计算机是如何工作的(简单介绍)
目录 一、冯诺依曼体系 二、CPU基本流程工作 逻辑⻔ 电⼦开关——机械继电器(Mechanical Relay) ⻔电路(Gate Circuit) 算术逻辑单元 ALU(Arithmetic & Logic Unit) 算术单元(ArithmeticUnit) 逻辑单元(Logic Unit) ALU 符号 寄存器(Regis…...
JSP基本表单和Request对象使用例子
表单的jsp; <%page contentType"text/html;charsetgbk" pageEncoding"UTF-8"%> <!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetUTF-8"><titl…...
【Redux】Redux 基本使用
1. Redux 快速上手 Redux 是 React 最常用的集中状态管理工具,类似于Vue中的Pinia(Vuex),可以独立于框架运行。 <button id"decrement">-</button> <span id"count">0</span> <…...
多线程Thread(初阶一:认识线程)
目录 一、引用线程的原因 二、线程的概念 三、进程和线程的区别 四、多线程编程 一、引用线程的原因 多任务操作系统,希望系统能同时运行多个任务。所以会涉及到进程,需要对进程进行管理、调度等。 而单任务操作系统,就完全不涉及到进程…...
系列五、GC垃圾回收【四大垃圾算法-复制算法】
一、堆的内存组成 二、复制算法 2.1、发生位置 复制算法主要发生在新生代,发生在新生代的垃圾回收也被叫做Minor GC。 2.2、 Minor GC的过程 复制>清空》交换 1、eden、from区中的对象复制到to区,年龄1 首先,当eden区满的时候会触发第一…...
LeetCode(24)文本左右对齐【数组/字符串】【困难】
目录 1.题目2.答案3.提交结果截图 链接: 文本左右对齐 1.题目 给定一个单词数组 words 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。 你应该使用 “贪心算法” 来放置给定的单…...
Spring-Spring之事务底层源码解析
EnableTransactionManagement工作原理 开启Spring事务本质上就是增加了一个Advisor,但我们使用EnableTransactionManagement注解来开启Spring事务是,该注解代理的功能就是向Spring容器中添加了两个Bean: AutoProxyRegistrarProxyTransactio…...
后端面经学习自测(三)
文章目录 1、ArrayList和Linkedlist区别?2、ArrayList扩容机制?3、ArrayList和Linkedlist分别能做什么场景?4、事务特性?MySQL事务Redis事务Spring事务5、在Spring中事务失效的场景?6、Java泛型?7、泛型擦除…...
力扣labuladong——一刷day40
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、力扣341. 扁平化嵌套列表迭代器 前言 N叉树的结构,构造迭代器 一、力扣341. 扁平化嵌套列表迭代器 /*** // This is the interface that allo…...
在VS Code中使用VIM
文章目录 安装和基本使用设置 安装和基本使用 VIM是VS Code的强大对手,其简化版本VI是Linux内置的文本编辑器,堪称VS Code问世之前最流行的编辑器,也是VS Code问世之后,我仍在使用的编辑器。 对VIM无法割舍的原因有二࿰…...
注解【元数据,自定义注解等概念详解】(超简单的好吧)
注解的理解与使用 注解的释义元数据的含义基础阶段常见的注解注解的作用(包括但不限于)教你读懂注解内部代码内容五种元注解尝试解读简单注解我当时的疑惑点 自定义注解自定义注解举例 注解的原理总结 注解的释义 我们都知道注释是拿来给程序员看的&…...
vue-pdf在vue框架中的使用
在components目录下新建PdfViewer/index.vue vue-pdf版本为4.3.0 <template><div :id"containerId" v-if"hasProps" class"container"><div class"right-btn"><div class"pageNum"><input v-m…...
Wordpress页面生成器:Elementor 插件制作网站页面教程(图文完整)
本文来教大家怎么使用Wordpress Elementor页面编辑器插件来自由创建我们的网页内容。很多同学在面对建站的时候,一开始都是热血沸腾信心满满的,等到实际上手的时候就会发现有很多问题都是无法解决的,希望本篇Elementor插件使用指南能够帮助到你。 Wordpress Elementor页面编…...
完全随机设计的方差分析
一、说明 实验设计在科学研究中发挥着至关重要的作用,使研究人员能够从数据中得出有意义的结论。一种常见的实验设计是完全随机设计(CRD),其特征是将实验单元随机分配到治疗组。CRD 的方差分析 (ANOVA) 是一种统计技术,…...
035、目标检测-物体和数据集
之——物体检测和数据集 目录 之——物体检测和数据集 杂谈 正文 1.目标检测 2.目标检测数据集 3.目标检测和边界框 4.目标检测数据集示例 杂谈 目标检测是计算机视觉中应用最为广泛的,之前所研究的图片分类等都需要基于目标检测完成。 在图像分类任务中&am…...
【开源】基于Vue.js的社区买菜系统的设计和实现
项目编号: S 011 ,文末获取源码。 \color{red}{项目编号:S011,文末获取源码。} 项目编号:S011,文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、系统设计2.1 功能模块设计2.1.1 数据中心模块2.1…...
【双指针】复写0
复写0 1089. 复写零 - 力扣(LeetCode) 给你一个长度固定的整数数组 arr ,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。 注意:请不要在超过该数组长度的位置写入元素。请对输入的数组 就地 进行上…...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...
手游刚开服就被攻击怎么办?如何防御DDoS?
开服初期是手游最脆弱的阶段,极易成为DDoS攻击的目标。一旦遭遇攻击,可能导致服务器瘫痪、玩家流失,甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案,帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...
【Linux】shell脚本忽略错误继续执行
在 shell 脚本中,可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行,可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令,并忽略错误 rm somefile…...
Spring AI 入门:Java 开发者的生成式 AI 实践之路
一、Spring AI 简介 在人工智能技术快速迭代的今天,Spring AI 作为 Spring 生态系统的新生力量,正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务(如 OpenAI、Anthropic)的无缝对接&…...
网络编程(UDP编程)
思维导图 UDP基础编程(单播) 1.流程图 服务器:短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...
JVM 内存结构 详解
内存结构 运行时数据区: Java虚拟机在运行Java程序过程中管理的内存区域。 程序计数器: 线程私有,程序控制流的指示器,分支、循环、跳转、异常处理、线程恢复等基础功能都依赖这个计数器完成。 每个线程都有一个程序计数…...
面向无人机海岸带生态系统监测的语义分割基准数据集
描述:海岸带生态系统的监测是维护生态平衡和可持续发展的重要任务。语义分割技术在遥感影像中的应用为海岸带生态系统的精准监测提供了有效手段。然而,目前该领域仍面临一个挑战,即缺乏公开的专门面向海岸带生态系统的语义分割基准数据集。受…...
【JVM】Java虚拟机(二)——垃圾回收
目录 一、如何判断对象可以回收 (一)引用计数法 (二)可达性分析算法 二、垃圾回收算法 (一)标记清除 (二)标记整理 (三)复制 (四ÿ…...
[大语言模型]在个人电脑上部署ollama 并进行管理,最后配置AI程序开发助手.
ollama官网: 下载 https://ollama.com/ 安装 查看可以使用的模型 https://ollama.com/search 例如 https://ollama.com/library/deepseek-r1/tags # deepseek-r1:7bollama pull deepseek-r1:7b改token数量为409622 16384 ollama命令说明 ollama serve #:…...
BLEU评分:机器翻译质量评估的黄金标准
BLEU评分:机器翻译质量评估的黄金标准 1. 引言 在自然语言处理(NLP)领域,衡量一个机器翻译模型的性能至关重要。BLEU (Bilingual Evaluation Understudy) 作为一种自动化评估指标,自2002年由IBM的Kishore Papineni等人提出以来,…...
