之前分享了教务系统中的系统管理模块的具体内容,相信或多或少都收获到了一些东西,今天继续讲述教务系统下的邮件系统模块。首先说一下我写的时候的思路,该模块下主要包含写邮件、收邮件、发邮件、草稿箱、垃圾箱等五个主要功能,用户通过登录系统后,可以在收件箱中看到其他用户给自己发送的邮件信息,也可以点击写邮件进行邮件编写点击保存到草稿箱中统一管理,草稿箱里会展示出保存后的所有编辑的邮件信息,可以单击抄送按钮跳转到发件箱回显邮件信息并填写收件人的电子邮件地址进行发送操作,也可以点击删除按钮把邮件删除到垃圾箱中,在垃圾箱中进行彻底的删除操作。设计的邮件系统的业务流程就这样,接下来还是一一简述操作流程。
首先还是要准备好数据库中的表,sys_email表的建表语句如下:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sys_email
-- ----------------------------
DROP TABLE IF EXISTS `sys_email`;
CREATE TABLE `sys_email` (
`email_id` int NOT NULL AUTO_INCREMENT COMMENT '邮件id',
`email_title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮件标题',
`email_context` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮件内容',
`email_state` int NULL DEFAULT NULL COMMENT '邮件状态',
`email_user_id` int NULL DEFAULT NULL COMMENT '邮件创建者id',
`logical_delete_state` int NULL DEFAULT 1 COMMENT '逻辑删除字段,1代表未逻辑删除,0代表逻辑删除了',
PRIMARY KEY (`email_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 24 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
过程中会和用户表中的信息关联所以需要一张中间表来维护关系,sys_user_email建表语句如下:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sys_user_email
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_email`;
CREATE TABLE `sys_user_email` (
`user_email_id` int NOT NULL AUTO_INCREMENT COMMENT '用户邮件Id',
`user_id` int NULL DEFAULT NULL COMMENT '发件用户ID',
`email_id` int NULL DEFAULT NULL COMMENT '邮件Id',
`rec_user_id` int NULL DEFAULT NULL COMMENT '收件用户ID',
PRIMARY KEY (`user_email_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 20 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
下面开始实现功能,首先是收件箱展示用户自己收到别人发送过来的邮件,效果图如下:
会显示发件人的姓名和邮件内容,可以选择已读修改邮件状态或者删除邮件到垃圾箱里。控制层拿到前端页面传递过来的用户id去关系表里面匹配,把所有匹配的数据全封装到集合中返回给页面进行展示。收件箱列表展示核心代码参考如下:
/**
* 根据用户id收件箱显示自己的邮件列表
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req, HttpServletResponse resp){
//获取当前用户id
HttpSession session = req.getSession();
SysUser user1 = (SysUser)session.getAttribute("USER");
int User_id = user1.getUser_id();
//使用当前用户id与关系表中的接收者id匹配,拿出关系表对象
List<SysUserEmail> sysUserEmailList = sysUserEmailService.queryByUserId(User_id);
List<InBoxInfo> infos = new ArrayList<>();
//取出集合中的邮件id和user_id去查询邮件内容和发件人姓名
for (SysUserEmail sysUserEmail : sysUserEmailList) {
int email_id = sysUserEmail.getEmail_id();//关系表中对应的邮件id
int user_id = sysUserEmail.getUser_id();//关系表中对应的发送者id
SysEmail sysEmail = emailService.queryEmailByEmailId(email_id);
SysUser sysUser = sysUserService.queryUserById(user_id);
String name = sysUser.getName();
String email_title = sysEmail.getEmail_title();
String email_context = sysEmail.getEmail_context();
int email_state = sysEmail.getEmail_state();
InBoxInfo inBoxInfo = new InBoxInfo(name,email_title,email_context,email_state);
infos.add(inBoxInfo);
}
//返回数据
req.setAttribute("infos",infos);
// 跳转页面
return "view/email/inboxEmail.jsp";
}
其次是写邮件功能,效果图如下:
通过输入邮件标题和邮件内容然后保存到草稿箱中,主要就是通过获取前端页面上输入的内容通过邮件对象封装起来存入数据即可。写邮件核心参考代码如下:
/**
* 写件箱写邮件
* @param req
* @param resp
* @return
*/
public String writeEmail(HttpServletRequest req, HttpServletResponse resp){
String title = req.getParameter("title");
String context = req.getParameter("context");
String state = req.getParameter("state");
Integer email_state = Integer.valueOf(state);
HttpSession session = req.getSession();
SysUser user1 = (SysUser)session.getAttribute("USER");
int User_id = user1.getUser_id();
//默认设置成未读
// 封装数据
SysEmail sysEmail = new SysEmail();
sysEmail.setEmail_title(title);
sysEmail.setEmail_context(context);
sysEmail.setEmail_state(email_state);
sysEmail.setEmail_user_id(User_id);
// 添加邮件
int n = emailService.addEmail(sysEmail);
//拿出的用户邮件对象,判断存放的使用者id是否与当前user_id相等
SysUserEmail sysUserEmail = new SysUserEmail();
List<SysEmail> sysEmailList = emailService.queryAll();
// 添加关系表之前需要进行删除
int number = sysUserEmailService.deleteUserEmailByUser_id(User_id);
for (SysEmail email : sysEmailList) {
if(email.getEmail_user_id() == User_id){
sysUserEmail.setUser_id(User_id);
sysUserEmail.setEmail_id(email.getEmail_id());
int num = sysUserEmailService.addUserEmail(sysUserEmail);
}
}
return "view/email/writeEmail.jsp";
}
然后是草稿箱显示,效果图如下:
草稿箱接收写件箱保存过来的邮件信息,在该界面可以跳转到发件箱或者删除邮件到垃圾箱在该界面隐藏该条邮件信息。这里也是根据登录时存储的session对象里的用户id来查询并显示具体的信息。草稿显示核心代码参考截图如下:
在然后是垃圾箱显示,效果图如下:
垃圾箱显示之前模块里操作删除过的邮件信息,点击彻底删除后该邮件信息会从数据库中彻底清除。这里也是根据登录时存储的session对象里的用户id来查询并显示具体的信息。彻底删除就是获取邮件的id,然后通过邮件id匹配关系表删除,然后再删除邮件,最后刷新页面更新数据。垃圾箱核心参考代码截图:
最后是较为复杂的发件箱功能,效果图如下:
如果直接点击左边框栏的发件箱就会弹出像第一张效果图显示的一样是空白的发送邮件页面需要手动填写邮件信息(相当于写邮件功能),如果是点击草稿箱里的抄送到发件箱就会像第二张效果图显示的一样会回显之前编辑好的邮件内容,填写好对应收件人的电子邮件,点击发送就可以发送给对方了。发件箱核心代码如下:
/**
* 点击发件箱直接跳转发件箱界面
* @param req
* @param resp
* @return
*/
public String outboxEmail(HttpServletRequest req, HttpServletResponse resp){
return "view/email/outboxEmailUI.jsp";
}
/**
* 草稿箱抄送到发件箱界面
* @param req
* @param resp
* @return
*/
public String outboxEmailUI(HttpServletRequest req, HttpServletResponse resp){
//接收数据
String email_idStr = req.getParameter("id");
System.out.println(email_idStr);
Integer email_id = Integer.valueOf(email_idStr);
// 根据获取的邮件id查找邮件对象
SysEmail sysEmail = emailService.queryEmailByEmailId(email_id);
System.out.println("发件信息" + sysEmail);
req.setAttribute("sysEmail",sysEmail);
return "view/email/outboxEmail.jsp";
}
/**
* 发送邮件
* @param req
* @param resp
* @return
*/
public String postEmail(HttpServletRequest req, HttpServletResponse resp){
HttpSession session = req.getSession();
SysUser user1 = (SysUser)session.getAttribute("USER");
int User_id = user1.getUser_id();
//接收数据
//通过邮件号获取接收者id
String emailAddress = req.getParameter("emailAddress");
SysUser emailReceiver = sysUserService.queryUserByEmail(emailAddress);
//拿到当前邮件对象
String email_idStr = req.getParameter("email_id");
//判断是否存在邮件id,若不存在就添加
if(email_idStr == ""){
String email_title = req.getParameter("email_title");
String email_context = req.getParameter("email_context");
//默认设置成未读
// 封装数据
SysEmail sysEmail = new SysEmail();
sysEmail.setEmail_title(email_title);
sysEmail.setEmail_context(email_context);
sysEmail.setEmail_state(0);
sysEmail.setEmail_user_id(User_id);
sysEmail.setLogical_delete_state(1);
// 添加邮件
int n = emailService.addEmail(sysEmail);
//拿出的用户邮件对象,判断存放的使用者id是否与当前user_id相等
SysUserEmail sysUserEmail = new SysUserEmail();
List<SysEmail> sysEmailList = emailService.queryAll();
// 添加关系表之前需要进行删除
int number = sysUserEmailService.deleteUserEmailByUser_id(User_id);
for (SysEmail email : sysEmailList) {
if(email.getEmail_user_id() == User_id){
sysUserEmail.setUser_id(User_id);
sysUserEmail.setEmail_id(email.getEmail_id());
sysUserEmail.setRec_user_id(emailReceiver.getUser_id());
int num = sysUserEmailService.addUserEmail(sysUserEmail);
}
}
}
else{
Integer email_id = Integer.valueOf(email_idStr);
SysEmail sysEmail = emailService.queryEmailByEmailId(email_id);
//把两个对象信息添加到关系表中
SysUserEmail sysUserEmail = new SysUserEmail();
sysUserEmail.setEmail_id(sysEmail.getEmail_id());
sysUserEmail.setUser_id(User_id);
sysUserEmail.setRec_user_id(emailReceiver.getUser_id());
sysUserEmailService.addUserEmail(sysUserEmail);
}
return "view/email/outboxEmailUI.jsp";
}
到此该教务系统中的基础版邮件系统就完成了。
标签:sysEmail,JavaWeb,教务,req,id,int,Servlet,email,邮件 From: https://blog.51cto.com/u_15101214/7061155