问答平台(3),发布帖子
AJAX
- Asynchronous JavaScript and XML
- 异步的JavaScript与XML,不是一门新技术,只是一个新的术语。
- 使用AJAX,网页能够将增量更新呈现在页面上,而不需要刷新整个页面。
- 虽然X代表XML,但目前JSON的使用比XML更加普遍。
封装 Json 工具
- 使用阿里的fastjson,引入依赖。
1
2
3
4
5<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency> - 工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20// CommunityUtil.java
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();
}
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 示例
- 使用jQuery发送AJAX请求。
表现层
1 |
|
页面
1 |
|
实战
- 采用 AJAX 请求,实现发布帖子的功能。
数据访问层
1 |
|
- discusspost-mapper.xml 里写sql
1
2
3
4
5
6
7<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>
业务层
1 |
|
表现层
1 |
|
页面
1 |
|
1 |
|
参考资料
问答平台(3),发布帖子
https://lcf163.github.io/2020/05/08/问答平台(3),发布帖子/