需求介绍

使用 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开发十五-发布帖子的更多相关文章

  1. STC8H开发(十五): GPIO驱动Ci24R1无线模块

    目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...

  2. SpringBoot开发十六-帖子详情

    需求介绍 实现帖子详情,在帖子标题上增加访问详情页面的链接. 代码实现 开发流程: 首先在数据访问层新增一个方法 实现查看帖子的方法 业务层同理增加查询方法 最后在表现层处理查询请求 数据访问层增加根 ...

  3. SpringBoot第十五篇:swagger构建优雅文档

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11007470.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前面的十四 ...

  4. SpringBoot | 第十五章:基于Postman的RESTful接口测试

    前言 从上一章节开始,接下来的几个章节会讲解一些开发过程中配套工具的使用.俗话说的好,工欲善其事,必先利其器.对于开发人员而言,有个好用的工具,也是一件事半功倍的事,而且开发起来也很爽,效率也会提升很 ...

  5. SpringBoot第二十五篇:SpringBoot与AOP

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   作者在实际 ...

  6. SpringBoot开发十九-添加评论

    需求介绍 熟悉事务管理,并且应用到添加评论的功能. 数据层:增加评论数据,修改帖子的评论数量 业务层:处理添加评论的业务,先增加评论再更新帖子的评论数量(因为用到了两个DML操作所以要用到事务管理) ...

  7. Java微信公众平台开发(十五)--微信JSSDK的使用

    转自:http://www.cuiyongzhi.com/post/63.html 在前面的文章中有介绍到我们在微信web开发过程中常常用到的 [微信JSSDK中Config配置] ,但是我们在真正的 ...

  8. SpringBoot | 第二十五章:日志管理之自定义Appender

    前言 前面两章节我们介绍了一些日志框架的常见配置及使用实践.一般上,在开发过程中,像log4j2.logback日志框架都提供了很多Appender,基本上可以满足大部分的业务需求了.但在一些特殊需求 ...

  9. SpringBoot开发十八-显示评论

    需求介绍 显示评论,还是我们之前做的流程. 数据层:根据实体查询一页的评论数据,以及根据实体查询评论的数量 业务层:处理查询评论的业务,处理查询评论数量的业务 表现层:同时显示帖子详情数据时显示该帖子 ...

随机推荐

  1. Spring:在web.xml正确加载spring配置文件的方式

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1. 如果spring配置文件的名称为applicationContext.xml,并 ...

  2. fail-fast 与 fail-safe

    fail-fast: fail-fast(快速失败),是Java集合的一种错误检测机制.当在遍历集合的过程中该集合在结构(改变集合大小)上发生变化时候,有可能发生fail-fast(快速失败行为不能得 ...

  3. SpringMvc实现批量删除,使用post传值一直报404错误

    Ajax结合SpringMVC实现批量删除信息,在前台使用post向后台传递要删除的id的集合额时候,一直报404错误, 前台post传值的源码如下: 了解一下: (1)第二行的rows为前面得到的一 ...

  4. ctf Decode

    这题其实没啥好说的,就是直接解,听学长说好像网上有现成的轮子可以用,我太年轻了,手动写了个decode函数. 也成功得到了flag.嘿嘿开心

  5. 利用C语言判定用户输入数据从而给出结果(利用判定用户体重范围)同求最优解!!!

    例子: 要求:医务工作者通过广泛的调查和统计分析,根据成人的身高与体重因素给出了按"体质指数"进行判断的方法,具体如下: 体质指数t=体重 w/(身高h)2(w的单位为kg,h的单 ...

  6. python 交换变量的值 不需要借助第三个变量

    >>> a,b,c,d=1,2,3,4>>> a,b,c,d=d,c,b,a>>> print(a,b,c,d)4 3 2 1>>&g ...

  7. Redis的持久化机制你学会了吗

    大家都知道Redis经常被使用在缓存的场景中,那有没有想过这么一个问题,一旦服务器宕机,内存中的数据全部丢失,我们该如何进行恢复呢?如果直接从后端数据库恢复,不仅会给数据库带来巨大的压力,还会使上层应 ...

  8. IO流之节点流(字符流)和数据流关闭

    ​输入流----Reader 1 public class Reader { 2 public static void main(String[] args) throws Exception { 3 ...

  9. 离散数学-传递闭包(POJ3275)

    就是n的元素给定m个关系求他们之间的关系. eg.  ∵a>b and b>c ∴a>c emmmm 若要知道n个元素的绝对关系,则需知道C(n,2)个关系. 例题:POJ3275 ...

  10. mysql为什么用b+树做索引

    关键字就是key的意思 一.B-Tree的性质 1.定义任意非叶子结点最多只有M个儿子,且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点的儿子数为[M/2, M]: ...