1、在html里
class使用.
id使用#
2、记得引入响应依赖(举例lombok)
3、messageController
package com.example.demo.demos.web;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/message")
@RestController
public class massageController {
//学过数据库的可以将这里改为数据库操作
public List<MessageInfo> messageInfos=new ArrayList<>();
//提交留言
@RequestMapping("/publish")
public boolean publishMessage(MessageInfo messageInfo){
if(!StringUtils.hasLength(messageInfo.getMessage())
||!StringUtils.hasLength(messageInfo.getTo())
||!StringUtils.hasLength(messageInfo.getFrom())
){
return false;
}
//暂时存放在内存中
messageInfos.add(messageInfo);
return true;
}
@RequestMapping("/getlist")
public List<MessageInfo> getlist(){
return messageInfos;
}
}
4、MessageInfo
package com.example.demo.demos.web;
import lombok.Data;
import lombok.Getter;
//可以省去我们的get和set方法,其他类可以直接调用
@Data
public class MessageInfo {
private String from;
private String to;
private String message;
}
5、messagewall前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>留言板</title>
<style>
.container {
width: 350px;
height: 300px;
margin: 0 auto;
/* border: 1px black solid; */
text-align: center;
}
.grey {
color: grey;
}
.container .row {
width: 350px;
height: 40px;
display: flex;
justify-content: space-between;
align-items: center;
}
.container .row input {
width: 260px;
height: 30px;
}
#submit {
width: 350px;
height: 40px;
background-color: orange;
color: white;
border: none;
margin: 10px;
border-radius: 5px;
font-size: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>留言板</h1>
<p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
<div class="row">
<span>谁:</span> <input type="text" name="" id="from">
</div>
<div class="row">
<span>对谁:</span> <input type="text" name="" id="to">
</div>
<div class="row">
<span>说什么:</span> <input type="text" name="" id="say">
</div>
<input type="button" value="提交" id="submit" onclick="submit()">
<!-- <div>A 对 B 说: hello</div> -->
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
//页面加载后信息留在页面上
$.ajax({
type:"get",
url:"/message/getlist",
success:function(messages){
for(var message of messages){
var html="<div>"+message.from+"对 "+message.to+" 说: "+message.message+"</div> "
$(".container").append(html);
}
}
});
function submit(){
//1. 获取留言的内容
var from = $('#from').val();
var to = $('#to').val();
var say = $('#say').val();
if (from== '' || to == '' || say == '') {
return;
}
$.ajax({
type:"post",
url:"/message/publish",
data:{
from:from,
to:to,
message:say
},
success:function(result){
if(result==true){
//添加成功
//2. 构造节点
var divE = "<div>"+from +"对" + to + "说:" + say+"</div>";
//3. 把节点添加到页面上
$(".container").append(divE);
//4. 清空输入框的值
$('#from').val("");
$('#to').val("");
$('#say').val("");
}else{
//弹一个框
alert("发表失败")
}
}
});
}
</script>
</body>
</html>
6、前端代码中涉及到的后端代码部分说明
(1)前端获取留言内容部分
(2)防止页面刷新之后我们上次写的话消失,我们对其进行一下保存
7、效果展示