基于ssm框架的留言板代码
时间: 2023-11-06 15:18:37 浏览: 54
以下是基于SSM框架的留言板代码:
1、留言实体类Message.java:
```java
package com.example.entity;
import java.util.Date;
public class Message {
private Integer id;
private String username;
private String content;
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
```
2、留言Dao接口MessageDao.java:
```java
package com.example.dao;
import com.example.entity.Message;
import java.util.List;
public interface MessageDao {
void addMessage(Message message);
List<Message> getMessageList();
}
```
3、留言Dao接口实现类MessageDaoImpl.java:
```java
package com.example.dao.impl;
import com.example.dao.MessageDao;
import com.example.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
@Repository
public class MessageDaoImpl implements MessageDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addMessage(Message message) {
String sql = "insert into message(username, content, create_time) values (?, ?, ?)";
jdbcTemplate.update(sql, message.getUsername(), message.getContent(), new Date());
}
@Override
public List<Message> getMessageList() {
String sql = "select * from message order by create_time desc";
return jdbcTemplate.query(sql, (rs, rowNum) -> {
Message message = new Message();
message.setId(rs.getInt("id"));
message.setUsername(rs.getString("username"));
message.setContent(rs.getString("content"));
message.setCreateTime(rs.getTimestamp("create_time"));
return message;
});
}
}
```
4、留言Service接口MessageService.java:
```java
package com.example.service;
import com.example.entity.Message;
import java.util.List;
public interface MessageService {
void addMessage(Message message);
List<Message> getMessageList();
}
```
5、留言Service接口实现类MessageServiceImpl.java:
```java
package com.example.service.impl;
import com.example.dao.MessageDao;
import com.example.entity.Message;
import com.example.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageDao messageDao;
@Override
public void addMessage(Message message) {
messageDao.addMessage(message);
}
@Override
public List<Message> getMessageList() {
return messageDao.getMessageList();
}
}
```
6、控制器MessageController.java:
```java
package com.example.controller;
import com.example.entity.Message;
import com.example.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class MessageController {
@Autowired
private MessageService messageService;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addMessage(Message message) {
messageService.addMessage(message);
return "redirect:/list";
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String getMessageList(ModelMap modelMap) {
List<Message> messageList = messageService.getMessageList();
modelMap.addAttribute("messageList", messageList);
return "list";
}
}
```
7、留言界面add.jsp:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加留言</title>
</head>
<body>
<h3>添加留言</h3>
<form action="${pageContext.request.contextPath}/add" method="post">
用户名:<input type="text" name="username"><br>
内容:<input type="text" name="content"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
8、留言列表界面list.jsp:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>留言列表</title>
</head>
<body>
<h3>留言列表</h3>
<a href="${pageContext.request.contextPath}/add">添加留言</a><br><br>
<table border="1">
<tr>
<td>用户名</td>
<td>留言内容</td>
<td>留言时间</td>
</tr>
<c:forEach var="message" items="${messageList}">
<tr>
<td>${message.username}</td>
<td>${message.content}</td>
<td>${message.createTime}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
以上就是基于SSM框架的留言板代码。
阅读全文