【SpringBoot】DEMO:实战③——留⾔板发布与展⽰功能实现【SpringBoot】DEMO:实战③——留⾔板发布与展⽰功能实现
⼀、设计思路
1. 发布留⾔:在发表页⾯,获取Title,Content,从Cookie中获取token,从token获取urname,把Title,Content,urname存
⼊数据库
2. 展⽰留⾔:扫描数据库,把留⾔表中的信息遍历,展⽰在⾸页
⼆、实现
1. 新建浏览表
2. 新建发布页⾯,页⾯使⽤了Template Layout,参考.
<!DOCTYPE html>
<html lang="en"th="www.thymeleaf">
<link rel="stylesheet"href="css/bootstrap.min.css">
<link rel="stylesheet"href="css/bootstrap-theme.min.css">
<link rel="stylesheet"href="css/community.css">
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/bootstrap.min.js"type="application/javascript"></script>
<head>
<meta chart="UTF-8">
门卫管理制度及岗位职责
<title>发布页⾯</title>
</head>
<body>
<!--导航栏-->
<div inrt="~{navication :: nav}"></div>
<div class="container-fluid main">
<div class="row">
<div class="col-lg-9 col-md-12 col-sm-12 col-xs-12">
<h2><span class="glyphicon glyphicon-plus"aria-hidden="true">发起</span></h2>
<hr>
<form action="/doPublish"method="post">
<div class="form-group">
<label for="title">问题标题</label>
<input text="${title}"type="text"class="form-control"id="title"name="title"placeholder="你想表达的标题......">
</div>
<div class="form-group">
<label for="content">问题内容</label>
大衣的拼音<textarea text="${content}"name="content"id="content"class="form-control"cols="30"rows="10"placeholder="你想表达的内容......"></text area>
</div>
<div class="container-fluid mian">
<div class="row">
<div class="col-lg-3 col-md-12 col-sm-12 col-xs-12">
<span text="${noTitle}"></span>朱明元
<span text="${noContent}"></span>
我喜欢的季节秋天
<button type="submit"class="btn btn-success btn-publish">发布</button>
</div>
</div>
</div>
</form>
</div>
<div class="col-lg-3 col-md-12 col-sm-12 col-xs-12">
<h3>发起问题指南</h3>
* 1 <br>
* 2 <br>
* 3 <br>
* 4 <br>
</div>
</div>
</div>
</body>
</html>
3. 控制层,PublishController
ample.homework.IndexController;
@Controller
public class PublishController {
@Autowired
private MessageMapper messageMapper;
@Autowired
private UrMapper urMapper;
//跳转⾄发布页⾯
@GetMapping("/publish")
public String Publish(){
return"publish";山东菏泽曹县
}
//发布功能实现
@PostMapping("/doPublish")
public String doPublish(
@RequestParam(value ="title") String title,
@RequestParam(value ="content") String content, HttpServletRequest request,
Model model
){
if(title == null || title ==""){
model.addAttribute("noTitle","标题不能为空!");
return"publish";
}
if(content == null || content ==""){
model.addAttribute("noContent","内容不能为空!");
return"publish";
}
Ur publish_ur = null;
Cookie[] cookies = Cookies();
for(Cookie cookie : cookies){
Name().equals("token")){
String token = Value();
publish_ur = urMapper.findByToken(token);
break;
}
}
Message message =new Message();
杜鹃花养殖message.tTitle(title);
message.tContent(content);
message.tUrname(Urname()); messageMapper.addMessage(message);
return"redirect:/";
}
}
4. Model层,Message
public class Message {
private Integer id;
private String urname;
private String title;
private String content;
private String token;
public Integer getId(){
return id;
}
public void tId(Integer id){
this.id = id;
}
public String getUrname(){
范蠡简介
return urname;
}
public void tUrname(String urname){
this.urname = urname;
}
public String getTitle(){
return title;
}
public void tTitle(String title){
this.title = title;
}
public String getContent(){
return content;
}
public void tContent(String content){
}
public String getToken(){
return token;
}
public void tToken(String token){
船越义珍}
}
5、 Mapper层,MessageMapper
import java.util.List;
@Mapper
@Repository
public interface MessageMapper {
//发布留⾔,插⼊数据库
@Inrt("inrt into message(title,content,urname) values (#{title},#{content},#{urname})") void addMessage(Message message);
//通过token寻找urname,为“发布留⾔”功能提供urname
@Select("lect * from ur where token = #{token}")
Ur findByToken(@Param("token") String token);
//展⽰所有留⾔
@Select("lect * from message")
List<Message>showAllMessage();
}
三、效果展⽰
完⼯!