Skip to content

Commit 71628fb

Browse files
committed
Update:
集成 publish.html
1 parent 4c922f8 commit 71628fb

File tree

6 files changed

+205
-2
lines changed

6 files changed

+205
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ create table USER
6464
primary key (ID)
6565
);
6666

67+
-- ----------------------------
68+
-- Table structure for Question
69+
-- ----------------------------
70+
create table question
71+
(
72+
id int auto_increment,
73+
title varchar(50),
74+
description text,
75+
gmt_create bigint,
76+
gmt_modified bigint,
77+
creator int,
78+
comment_count int default 0,
79+
view_count int default 0,
80+
like_count int default 0,
81+
tag varchar(256),
82+
constraint question_pk
83+
primary key (id)
84+
);
85+
86+
6787
```
6888

6989

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
11
package life.majiang.community.controller;
22

3+
import life.majiang.community.mapper.QuestionMapper;
4+
import life.majiang.community.mapper.UserMapper;
5+
import life.majiang.community.model.Question;
6+
import life.majiang.community.model.User;
7+
import org.springframework.beans.factory.annotation.Autowired;
38
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
410
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
14+
import javax.servlet.http.Cookie;
15+
import javax.servlet.http.HttpServletRequest;
516

617
@Controller
718
public class PublishController {
19+
20+
@Autowired // 注入 questionMapper
21+
private QuestionMapper questionMapper;
22+
23+
@Autowired // 注入 userMapper
24+
private UserMapper userMapper;
25+
26+
// get 去渲染页面
827
@GetMapping("/publish")
9-
public String publish(){
28+
public String publish() {
1029
return "publish";
1130
}
31+
32+
// post 去执行请求
33+
@PostMapping("/publish")
34+
public String doQuestion(
35+
//接收参数
36+
@RequestParam("title") String title,
37+
@RequestParam("description") String description,
38+
@RequestParam("tag") String tag,
39+
HttpServletRequest request,
40+
Model model
41+
) {
42+
// 获取填写者信息
43+
User user = null;
44+
Cookie[] cookies = request.getCookies();
45+
for (Cookie cookie : cookies) {
46+
if (cookie.getName().equals("token")) { // 检查 cookies_key是否为 token
47+
String token = cookie.getValue();
48+
user = userMapper.findByToken(token);
49+
// 如果user != null 写入session
50+
if (user != null) {
51+
request.getSession().setAttribute("user", user);
52+
}
53+
break; // 命中后结束循环
54+
}
55+
}
56+
57+
if (user == null) {
58+
model.addAttribute("error", "用户未登录");
59+
return "publish";// 有问题返回 piblish
60+
}
61+
62+
// 获取参数
63+
Question question = new Question();
64+
question.setTitle(title);
65+
question.setDescription(description);
66+
question.setTag(tag);
67+
question.setCreator(user.getId());
68+
questionMapper.create(question);
69+
return "redirect:/"; // 成功返回 首页
70+
}
1271
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package life.majiang.community.mapper;
2+
3+
import life.majiang.community.model.Question;
4+
import org.apache.ibatis.annotations.Insert;
5+
import org.apache.ibatis.annotations.Mapper;
6+
7+
@Mapper
8+
public interface QuestionMapper {
9+
@Insert("insert into question (title, description, gmt_create, gmt_modified, creator, tag) " +
10+
"values (#{title}, #{description}, #{gmtCreate}, #{gmtModified}, #{creator}, #{tag})")
11+
void create(Question question);
12+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package life.majiang.community.model;
2+
3+
public class Question {
4+
private Integer id; // 问题自增id
5+
private String title; // title
6+
private String description; // 描述
7+
private String tag; // 标签
8+
private Long gmtCreate; // 创建时间
9+
private Long gmtModified; // 更新时间
10+
private Integer creator; // 创建者
11+
private Integer viewCount; // 阅读数
12+
private Integer commentCount; // 提交数
13+
private Integer likeCount; // 点赞数
14+
15+
public Integer getId() {
16+
return id;
17+
}
18+
19+
public void setId(Integer id) {
20+
this.id = id;
21+
}
22+
23+
public String getTitle() {
24+
return title;
25+
}
26+
27+
public void setTitle(String title) {
28+
this.title = title;
29+
}
30+
31+
public String getDescription() {
32+
return description;
33+
}
34+
35+
public void setDescription(String description) {
36+
this.description = description;
37+
}
38+
39+
public String getTag() {
40+
return tag;
41+
}
42+
43+
public void setTag(String tag) {
44+
this.tag = tag;
45+
}
46+
47+
public Long getGmtCreate() {
48+
return gmtCreate;
49+
}
50+
51+
public void setGmtCreate(Long gmtCreate) {
52+
this.gmtCreate = gmtCreate;
53+
}
54+
55+
public Long getGmtModified() {
56+
return gmtModified;
57+
}
58+
59+
public void setGmtModified(Long gmtModified) {
60+
this.gmtModified = gmtModified;
61+
}
62+
63+
public Integer getCreator() {
64+
return creator;
65+
}
66+
67+
public void setCreator(Integer creator) {
68+
this.creator = creator;
69+
}
70+
71+
public Integer getViewCount() {
72+
return viewCount;
73+
}
74+
75+
public void setViewCount(Integer viewCount) {
76+
this.viewCount = viewCount;
77+
}
78+
79+
public Integer getCommentCount() {
80+
return commentCount;
81+
}
82+
83+
public void setCommentCount(Integer commentCount) {
84+
this.commentCount = commentCount;
85+
}
86+
87+
public Integer getLikeCount() {
88+
return likeCount;
89+
}
90+
91+
public void setLikeCount(Integer likeCount) {
92+
this.likeCount = likeCount;
93+
}
94+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
create table question
2+
(
3+
id int auto_increment,
4+
title varchar(50),
5+
description text,
6+
gmt_create bigint,
7+
gmt_modified bigint,
8+
creator int,
9+
comment_count int default 0,
10+
view_count int default 0,
11+
like_count int default 0,
12+
tag varchar(256),
13+
constraint question_pk
14+
primary key (id)
15+
);
16+

src/main/resources/templates/publish.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<div class="col-lg-9 col-md-12 col-sm-12 col-xs-12">
5858
<h2><span aria-hidden="true" class="glyphicon glyphicon-plus"></span>发起</h2>
5959
<hr>
60-
<from action="">
60+
<from action="/publish" method="post">
6161
<div class="form-group">
6262
<label for="titile">问题标题 (简明扼要):</label>
6363
<input class="form-control" id="titile" name="titile" placeholder="问题标题" type="text">
@@ -71,6 +71,8 @@ <h2><span aria-hidden="true" class="glyphicon glyphicon-plus"></span>发起</h2>
7171
<label for="titile">添加标签:</label>
7272
<input class="form-control" id="tag" name="tag" placeholder="输入标签 以,分隔" type="text">
7373
</div>
74+
75+
<span class="alert alert-danger" th:text="${error}"></span>
7476
<button class="btn btn-info btn-publish" type="submit">确认发布</button>
7577
</from>
7678
</div>

0 commit comments

Comments
 (0)