1. 大体思路
①异步请求
这次用到了AJAX,网页能够将增量更新呈现在页面上,而不需要刷新整个页面。
我们点击【发布帖子】按钮后,会出现一个弹窗,此时后面的页面并没有被刷新。
②流程
我们点击发布按钮后,publishBtn按钮会执行index.js中的publish()方法,跳转到:CONTEXT_PATH + "/discuss/add";
会执行控制器类 DiscussPostController 的 addDiscussPost()方法。里面调用Service: discussPostService,该service又调用了 discussPostMapper,通过其对应的SQL语句将帖子内容插进 discuss_post表中。
③开发流程
1.我们从最简单的工具类开始,在里面写上了我们需要的一些工具方法;
2. 在数据层dao中的接口声明了增删改查方法,并在对应的XXXmapper.xml文件中编写对应的SQL语句;
3. 业务的核心逻辑都在Service层,在service类中编写了一些需要的业务逻辑,最后调用dao里的方法,实现对数据层的更新。
4. Service之后,最后就是视图层的编写,分为两个部分:控制器 + 页面。
2. 引入AJAX依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
3. 工具类
在util.CommunityUtil类中添加新的工具方法:
返回状态码,在贴子发布后,显示发布成功。
//得到JSON格式的字符串
//输入为:编号、提示、业务数据
public static String getJSONString(int code, String msg, Map<String, Object> map){
JSONObject json = new JSONObject();
json.put("code",code);
json.put("msg",msg);
if (map!=null){
for (String key: map.keySet()) {
json.put(key, map.get(key));
}
}
return json.toJSONString();
}
//得到JSON格式的字符串(重载1:无业务数据)
public static String getJSONString(int code, String msg){
return getJSONString(code, msg, null);
}
//得到JSON格式的字符串(重载2:无提示、业务数据)
public static String getJSONString(int code){
return getJSONString(code, null, null);
}
4. 插入帖子功能
① dao.DiscussPostMapper接口新添加方法:
//添加帖子
int insertDiscussPost(DiscussPost discussPost);
② mapper.discusspost-mapper添加对应的SQL语句:
<insert id="insertDiscussPost" parameterType="DiscussPost">
insert into discuss_post(<include refid="insertFields"></include>)
values (#{userId},#{title},#{content},#{type},#{status},#{createTime},#{commentCount},#{score})
</insert>
5. Service
在service.DiscussPostService类下新建方法:addDiscussPost()。
@Autowired
private SensitiveFilter sensitiveFilter;
public int addDiscussPost(DiscussPost post){
if(post==null){
throw new IllegalArgumentException("参数不能为空!");
}
//转义HTML标记:防止人家发布的内容中包含html的标签,导致破坏页面
//只用对主题、评论进行转义、过滤操作
post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
post.setContent(HtmlUtils.htmlEscape(post.getContent()));
//过滤敏感词
post.setTitle(sensitiveFilter.filter(post.getTitle()));
post.setContent(sensitiveFilter.filter(post.getContent()));
return discussPostMapper.insertDiscussPost(post);
}
6. 控制器方法
在controller目录下新建:DiscussPostController。以后所有与发帖相关的请求都在这里处理。
package com.nowcoder.mycommunity.controller;
import com.nowcoder.mycommunity.entity.DiscussPost;
import com.nowcoder.mycommunity.entity.User;
import com.nowcoder.mycommunity.service.DiscussPostService;
import com.nowcoder.mycommunity.util.CommunityUtil;
import com.nowcoder.mycommunity.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
//处理所有与发帖相关的请求
@Controller
@RequestMapping("/discuss")
public class DiscussPostController {
@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){
// 403表示没有权限
return CommunityUtil.getJSONString(403, "你还没有登录哦!");
}
DiscussPost post = new DiscussPost();
post.setUserId(user.getId());
post.setTitle(title);
post.setContent(content);
post.setCreateTime(new Date());
discussPostService.addDiscussPost(post);
return CommunityUtil.getJSONString(0, "发布成功");
}
}
7. index.js
index.html中101行的【发布按钮】绑定了一个函数publish(),可以在Index.js中查看(双击shift搜索)。
$(function(){
$("#publishBtn").click(publish);
});
function publish() {
$("#publishModal").modal("hide");
// 获取标题和内容
var title = $("#recipient-name").val();
var content = $("#message-text").val();
// 发送异步请求(POST)
$.post(
CONTEXT_PATH + "/discuss/add",
{"title":title,"content":content},
function(data) {
data = $.parseJSON(data);
// 在提示框中显示返回消息
$("#hintBody").text(data.msg);
// 显示提示框
$("#hintModal").modal("show");
// 2秒后,自动隐藏提示框
setTimeout(function(){
$("#hintModal").modal("hide");
// 刷新页面
if(data.code == 0) {
window.location.reload();
}
}, 2000);
}
);
}