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开发十八-显示评论
需求介绍 显示评论,还是我们之前做的流程. 数据层:根据实体查询一页的评论数据,以及根据实体查询评论的数量 业务层:处理查询评论的业务,处理查询评论数量的业务 表现层:同时显示帖子详情数据时显示该帖子 ...
随机推荐
- SpringBoot:springboot项目jar包如何引入外置配置文件
springboot项目打成jar包,默认读取的classpath路径下的配置文件,config.properties是自定义配置文件. 如果要把config.properties配置 ...
- phpstorm之"Can not run PHP Code Sniffer"
前言 其实我是不太愿意写这种工具使用博客的,因为实在没有营养,只是有些简单问题,搜索一番,却始终找不到答案,遂以博客记录下来,希望后面的人,可以省去搜索之苦. 相信你搜到这篇博客,肯定是已经安装好了P ...
- Java多线程事务管理
今天要讨论的是"Java实现多线程单条数据事务管理",在此之前,顺便回顾一下实现多线程的几种方式 实现多线程的三种方式 一.继承Thread类 第一种方法是继承Thread类,重写 ...
- 性能基准DevOps之如何提升脚本执行效率
1.宝路说 宝路最近一直在自我思考:性能基准DevOps工作已经开展一段时间了,目前我们确实已经取得了一些成果,显然这还远远不够.趁闲暇之余跟组员进行了简单的头脑风暴!于是这就有了今天的主题,当然这仅 ...
- Linux常用命令 day day up
一.Shell二.Linux命令的分类1.查看内部命令2.禁用内部命令三.Linux命令行格式四.编辑Linux命令行的辅助操作五.获得命令帮助的方法1.pwd--查看当前的工作目录2.cd--切换工 ...
- C语言:九宫格
#include <stdio.h> /* 如下排列表示 A00 A01 A02 A10 A11 A12 A20 A21 A22 */ int main() { unsigned char ...
- python 模拟点击微信
from PyQt5 import QtCore,QtWidgets import win32gui, win32api, win32con # 调用win32api的模拟点击功能实现ctrl+v粘贴 ...
- C语言:字符编码
C语言是 70 年代的产物,那个时候只有 ASCII,各个国家的字符编码都还未成熟,所以C语言不可能从底层支持 GB2312.GBK.Big5.Shift-JIS 等国家编码,也不可能支持 Unico ...
- jieba分词处理
分词是一种数学上的应用,他可以直接根据词语之间的数学关系进行文字或者单词的抽象,比如,讲一句话"我来自地球上中国"进行单词分割,我们可能会得到如下的内容:"我" ...
- python + csv 操作(读写)
import csv"""与excel文件不同,csv文件中:1.数据都没有数据类型,值都是'字符串'2.没有颜色和样式,不能指定单元格测的宽高,不能合并单元格3.没有对 ...