留言功能界面
1、页面-添加留言
<form id="msg-form" method="post" action="${pageContext.request.contextPath}/msgAdd.action">
<div class="col-md-12">
<div class="form-group col-md-9 col-md-offset-1">
<label for="msg">Your message here:</label>
<textarea id="msg_text" class="form-control" rows="3" name="msg_text" placeholder="Enter Your message:" required></textarea>
</div>
<div class="form-group col-md-9 col-md-offset-1 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
2、点击提交按钮,添加留言,保存留言信息到数据库
a、下面是servlet的代码,处理添加留言的请求
@WebServlet(name = "MsgAddServlet",value = "/msgAdd.action")
public class MsgAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String msg_text = request.getParameter("msg_text");
MsgService msgService = new MsgServiceImpl();
UserService userService = new UserServiceImpl();
//获取当前登录的用户信息
HttpSession httpSession = request.getSession();
User user = (User) httpSession.getAttribute("user");
Cookie cookie = CookieUtil.getCookieByName(request, "front_name_pass");
System.out.println("cookie:"+cookie);
if(user == null ){
Boolean flg = false;
if(cookie != null) {
//有保存当前项目学号和密码的cookie
String namePass = URLDecoder.decode(cookie.getValue(),"UTF-8");
String[] namePass2 =namePass.split("#");
//获取保存在cookie中的用户名
User user2 = userService.findByName(namePass2[0]);
if (user2 != null && user2.getPassword().equals(namePass2[1]) && user2.getGroup_id().equals(1) && user2.getDisabled().equals(0)){
//用户正常使用,cookie中用户信息有效,登录成功
request.getSession().setAttribute("user",user2);
//保存留言到数据库
Msg msg = new Msg(msg_text,user2.getUser_id());
msgService.save(msg);
response.sendRedirect(request.getServletContext().getContextPath()+"/msg.action");
}else{
//未登录,要求先登录才能留言,跳转到登录界面
request.setAttribute("msg","请先登录,在留言!");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}else{
//未登录,要求先登录才能留言,跳转到登录界面
request.setAttribute("msg","请先登录,在留言!");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}else {
//保存留言到数据库
Msg msg = new Msg(msg_text,user.getUser_id());
msgService.save(msg);
response.sendRedirect(request.getServletContext().getContextPath()+"/msg.action");
}
}
}
b、servlet调用service中的方法
public class MsgServiceImpl implements MsgService {
MsgDao msgDao = new MsgDaoImpl();
@Override
public void save(Msg msg) {
msgDao.save(msg);
}
@Override
public List<Msg> findAll() {
return msgDao.findAll();
}
}
c、service调用dao层代码
@Override
public void save(Msg msg) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//①获取连接
connection = ConnectionFactory.getConnection();
//②准备sql语句
String sql ="INSERT INTO t_msg(msg_text,msg_time,user_id) VALUE(?,NOW(),?)";
//③ 获取集装箱
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1,msg.getMsg_text());
preparedStatement.setInt(2,msg.getUser_id());
//④执行查询,将查询出来的结果封装在resultSet,注意他这个地方会将表头信息查询出来
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//一般用于善后清理工作,比如关闭连接 集装箱 结果集
ConnectionFactory.close(connection,preparedStatement,resultSet);
}
}
2、页面-查询留言
<c:forEach items="${msgs}" var="msg">
<p class="story_t"> <font color="337ab7"> ${msg.user.username}</font>:${msg.msg_text}</p>
<p class="story_time">
${msg.msg_time}
<button class="btn btn-default" onclick="onValue( ${msg.msg_id})" data-target="#myModal" data-toggle="modal"
value="回复" style="background: url(${pageContext.request.contextPath}/image/reply.png) no-repeat;width: 30px;height: 15px;border: none;">
</button>
</p>
<c:forEach items="${msg.replies}" var="replie">
<p class="story_hf">@${replie.user.username}:${replie.reply_context}</p>
</c:forEach>
</c:forEach>
3、页面-点击回复出现回复的模态框
<!--fade 淡入淡出 回复留言的模态框-->
<div class="modal fade" id="myModal" aria-labelledby="myModallabel" aria-hidden="true" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" data-dismiss="modal" aria-hidden="true">×</button>
<h5 class="modal-title" id="myModallabel">回复信息</h5>
</div>
<form id="formData" class="form-horizontal" action="${pageContext.request.contextPath}/msgReply.action" method="get">
<div class="modal-body">
<div class="form-group">
<label class="control-label col-lg-2">留言:</label>
<div class="col-lg-9">
<input type="hidden" id="msg_id" name="msg_id" >
<textarea name="reply_context" id="reply_context" placeholder="请输入回复信息" class="form-control" rows="5" required></textarea> </br>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">close</button>
<button class="btn btn-success" type="submit" >提交</button>
</div>
</form>
</div>
</div>
</div>