SpringBoot开发十五-发布帖子
需求介绍
使用 AJAX 异步通信实现网页能够增量的更新呈现到页面上而不需要刷新整个页面。
现在基本上都是服务器返回 JSON 字符串来解析
代码实现
使用 JQuery 发送 AJAX 请求。
首先我们要有几个处理 JSON 字符串的方法,因为服务器要给浏览器返回 JSON 字符串,我们引入一个包下的 API 来处理。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
利用这个 API 开发工具方法,往往服务器向浏览器返回的JSON数据,这个JSON包含几个部分:编码,即时信息(成功还是失败啊这些),以及业务数据。
public static String getJSONString(int code, String msg, Map<String, Object> map) {
// 先定义一个JSON对象
JSONObject json = new JSONObject();
json.put("code", code);
json.put("msg", msg);
// 把Hashmap打散放到json里面
if (map != null) {
for (String key : map.keySet()) {
json.put(key, map.get(key));
}
}
return json.toJSONString();
}
public static String getJSONString(int code, String msg) {
return getJSONString(code, msg, null);
}
public static String getJSONString(int code) {
return getJSONString(code, null, null);
}
然后写一个实例模拟 AJAX 的异步请求,那么需要在 AlphaController 里面声明一个方法处理这样的请求,
// Ajax实例
@RequestMapping(path = "/ajax", method = RequestMethod.POST)
// 异步请求是不返回网页的,所以是返回body
@ResponseBody
public String testAjax(String name, int age) {
System.out.println(name);
System.out.println(age);
return CommunityUtil.getJSONString(0, "ok");
}
然后写页面处理,就不细致的说了。
现在就写发布帖子的功能。
那么就从数据访问层写起,那么要增加一个方法 insertDiscussPost 能增加帖子的数据
int insertDiscussPost(DiscussPost discussPost);
然后去 discusspost-mapper.xml 文件里面写方法的实现:
<sql id="insertFields">
user_id, title, content, type, status, create_time, comment_count, score
</sql> <insert id="insertDiscussPost" parameterType="DiscussPost" keyProperty="id">
insert into discuss_post(<include refid="insertFields"></include>)
values(#{userId},#{title},#{content},#{type},#{status},#{createTime},#{commentCount},#{score})
</insert>
数据访问层写完,写业务层,业务层需要提供一个能够保存帖子的业务方法 addDiscussPost,添加的帖子也要利用到之前写过的过滤敏感词的方法
// 先注入过滤敏感词的工具
@Autowired
private SensitiveFilter sensitiveFilter; public int addDiscussPost(DiscussPost discussPost) {
if (discussPost == null) {
throw new IllegalArgumentException("参数不能为空!");
}
// 转义Html标签
discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));
// 过滤敏感词
discussPost.setTitle(sensitiveFilter.filter(discussPost.getTitle()));
discussPost.setContent(sensitiveFilter.filter(discussPost.getContent())); return discussPostMapper.insertDiscussPost(discussPost);
}
然后新建 DiscussPostController,控制视图层,那么以后涉及到帖子的都在这个 Controller 里面实现
package com.nowcoder.community.controller; import com.nowcoder.community.dao.CommentMapper;
import com.nowcoder.community.entity.*;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.*; @Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {
@Autowired
private DiscussPostService discussPostService; // 获取当前用户
@Autowired
private HostHolder hostHolder; @RequestMapping(path = "/add", method = RequestMethod.POST)
@ResponseBody
public String addDiscussPost(String title, String content) {
User user = hostHolder.getUser();
if (user == null) {
return CommunityUtil.getJSONString(403, "您还没有登录");
}
DiscussPost discussPost = new DiscussPost();
discussPost.setUserId(user.getId());
discussPost.setTitle(title);
discussPost.setContent(content);
discussPost.setCreateTime(new Date());
discussPostService.addDiscussPost(discussPost); // 报错的情况以后统一处理
return CommunityUtil.getJSONString(0, "发布成功!");
}
}
然后就开始处理页面,要写异步请求,就可以了。

SpringBoot开发十五-发布帖子的更多相关文章
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...
- SpringBoot开发十六-帖子详情
需求介绍 实现帖子详情,在帖子标题上增加访问详情页面的链接. 代码实现 开发流程: 首先在数据访问层新增一个方法 实现查看帖子的方法 业务层同理增加查询方法 最后在表现层处理查询请求 数据访问层增加根 ...
- SpringBoot第十五篇:swagger构建优雅文档
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11007470.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 前面的十四 ...
- SpringBoot | 第十五章:基于Postman的RESTful接口测试
前言 从上一章节开始,接下来的几个章节会讲解一些开发过程中配套工具的使用.俗话说的好,工欲善其事,必先利其器.对于开发人员而言,有个好用的工具,也是一件事半功倍的事,而且开发起来也很爽,效率也会提升很 ...
- SpringBoot第二十五篇:SpringBoot与AOP
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 作者在实际 ...
- SpringBoot开发十九-添加评论
需求介绍 熟悉事务管理,并且应用到添加评论的功能. 数据层:增加评论数据,修改帖子的评论数量 业务层:处理添加评论的业务,先增加评论再更新帖子的评论数量(因为用到了两个DML操作所以要用到事务管理) ...
- Java微信公众平台开发(十五)--微信JSSDK的使用
转自:http://www.cuiyongzhi.com/post/63.html 在前面的文章中有介绍到我们在微信web开发过程中常常用到的 [微信JSSDK中Config配置] ,但是我们在真正的 ...
- SpringBoot | 第二十五章:日志管理之自定义Appender
前言 前面两章节我们介绍了一些日志框架的常见配置及使用实践.一般上,在开发过程中,像log4j2.logback日志框架都提供了很多Appender,基本上可以满足大部分的业务需求了.但在一些特殊需求 ...
- SpringBoot开发十八-显示评论
需求介绍 显示评论,还是我们之前做的流程. 数据层:根据实体查询一页的评论数据,以及根据实体查询评论的数量 业务层:处理查询评论的业务,处理查询评论数量的业务 表现层:同时显示帖子详情数据时显示该帖子 ...
随机推荐
- 2020年MySQL数据库面试题总结(50道题含答案解析)
1.MySQL 中有哪几种锁? (1)表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最 高,并发度最低. (2)行级锁:开销大,加锁慢:会出现死锁:锁定粒度最小,发生锁冲突的概率最 ...
- openjudge走迷宫(DFS)
题目: 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- IDA,IDA PRO 产品介绍
IDA理念这是我们在开发产品时竭尽全力遵循的理念--在此过程中,我们相信我们将开发出能够为您带来所需的可靠性.便利性和易用性的软件.没有什么能打败人脑因为我们知道一秒钟的洞察力仍然胜过百年的处理时间, ...
- Jmeter监控服务器CPU,Memory,Disk,Network性能指标
本文主要说一下如何通过JMeter插件来监控服务器CPU.内存.磁盘.网络等相关资源. 一.下载 第一种方案: 首先进入网址https://jmeter-plugins.org/downloads/o ...
- C语言代码段
/* 功 能:将str字符串中的oldstr字符串替换为newstr字符串 * 参 数:str:操作目标 oldstr:被替换者 newstr:替换者 * 返回值:返回替换之后的字符串 */ char ...
- ArchLinux安装步骤(一)
本文为安装archlinux的教程,需要有硬盘分区,挂载等基础linux命令的了解还有vim的基本操作,不知道也没关系,这里有大神的视频教程ArchLinux指南. 确实是不是uefi模式 ls /s ...
- I-Identical Day[题解]
原题目地址(牛客) Identical Day 题目大意 给定一个长度为 \(n\) 的 \(01\) 串,对于每段长度为 \(l\) 的连续的 \(1\) ,其权值为 \(\frac{l\times ...
- Spring Boot邮箱链接注册验证
Spring Boot邮箱链接注册验证 简单介绍 注册流程 [1]前端提交注册信息 [2]后端接受数据 [3]后端生成一个UUID做为token,将token作为redis的key值,用户数据作为re ...
- 基于JSP的学生考勤管理系统(MySQL版)
介绍:基于JSP的学生考勤管理系统(MySQL版)1.包含源程序,数据库脚本.代码和数据库脚本都有详细注释.2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善.开发环境:Eclipse ,MyS ...
- 3java基础补充(今天和昨天学习内容整理)
1.java单机项目 2.JavaSE又被称为J2SE,JavaEE和JavaME类同. 3.Java特性(总结) (1)跨平台/可移植性:相同的Java代码可以在任何一个支持的平台(操作系统)上运行 ...