【SpringBoot】DEMO:实战③——留言板发布与展示功能实现

更新时间:2023-07-01 12:15:14 阅读: 评论:0

【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();
}
三、效果展⽰
完⼯!

本文发布于:2023-07-01 12:15:14,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1063107.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:功能   问题   新建   发布页   标题   内容   发布
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图