关于修改留言的问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %> <%@ taglib prefix= "fmt" uri= "http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <meta charset= "UTF-8" > <title>我的留言</title> <link rel= "stylesheet" href= "../../../css/index.css" > <script type= "text/javascript" > function submitMessageForm(flag) { if ( 'first' == flag) { document.getElementById( 'page' ).value = 1 ; } else if ( 'pre' == flag) { var current = Number(document.getElementById( 'page' ).value); if (current > 1 ) { document.getElementById( 'page' ).value = current - 1 ; } } else if ( 'next' == flag) { var current = Number(document.getElementById( 'page' ).value); var last = Number(document.getElementById( 'last' ).value); if (current < last) { document.getElementById( 'page' ).value = current + 1 ; } } else if ( 'last' == flag) { var last = Number(document.getElementById( 'last' ).value); document.getElementById( 'page' ).value = last < 1 ? 1 : last; } document.getElementById( 'messageForm' ).submit(); } </script> </head> <body> <header> <div class = "container" > <% if ( null != request.getSession().getAttribute( "user" )) {%> <nav> <a class = "navbar-brand" href= "/message/list.do" > 返回留言板 </a> </nav> <nav> <a href= "/userInfo.do" >我的信息</a> </nav> <%} else { %> <nav> <a href= "/login.do" >登录</a> <a href= "/regPrompt.do" >注册</a> </nav> <% } %> </div> </header> <section class = "banner" > <div class = "container" > <div> <h1>慕课网留言板</h1> <p>慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人,也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。 </p> </div> </div> </section> <section class = "main" > <div class = "container" > <c:forEach items= "${messages}" var= "msg" > <div class = "alt-item" > <div class = "alt-head" > <div class = "alt-info" > <span>作者:<a href= "" >${msg.username}</a></span> <span>时间:<fmt:formatDate pattern= "yyyy-MM-dd HH:mm:ss" value= "${msg.createTime}" /></span> </div> </div> <div class = "alt-content" > <h3>${msg.title}</h3> <p>${msg.content}</p> </div> <div class = "alt-update" > <% if ( null != request.getSession().getAttribute( "user" )) {%> <span><a href= "/show/update/message.do?id=${msg.id}" >修改</a></span> <span><a href= "/delete/message.do?id=${msg.id}" >删除</a></span> <% }%> </div> </div> </c:forEach> </div> </section> <section class = "page" > <div class = "container" > <div id= "pagefy" > <ul> <form id= "messageForm" action= "/my/message/list.do?id=${user.id}" method= "post" > <input type= "hidden" id= "page" name= "page" value= "${page}" > <input type= "hidden" id= "last" name= "last" value= "${last}" > <li><a href= "javascript:void(0)" onclick= "submitMessageForm('first')" >首页</a></li> <li><a href= "javascript:void(0)" onclick= "submitMessageForm('pre')" >上一页</a></li> <li><a href= "javascript:void(0)" >当前第${page}页</a></li> <li><a href= "javascript:void(0)" onclick= "submitMessageForm('next')" >下一页</a></li> <li><a href= "javascript:void(0)" onclick= "submitMessageForm('last')" >尾页</a></li> </form> </ul> </div> </div> </section> <footer> copy@慕课网 </footer> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.imooc.servlet; import com.imooc.bean.Message; import com.imooc.service.MessageService; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Objects; /** * 修改的Servlet */ public class UpdateMessageServlet extends HttpServlet { private MessageService messageService; @Override public void init() throws ServletException { super .init(); messageService = new MessageService(); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathName = req.getServletPath(); if (Objects.equals( "/show/update/message.do" ,pathName)){ //根据ID从数据库中查询留言 String id = req.getParameter( "id" ); Message message = messageService.getMessageById(id); if ( null !=message){ req.setAttribute( "message" ,message); req.getRequestDispatcher( "/WEB-INF/views/biz/update_messagebyid.jsp" ).forward(req,resp); } else { } } else if (Objects.equals( "/update/message.do" ,pathName)){ //修改留言 String title = req.getParameter( "title" ); String content = req.getParameter( "content" ); Message message = new Message(); message.setTitle(title); message.setContent(content); boolean result = messageService.update(message); if (result){ req.setAttribute( "message" ,message); req.getRequestDispatcher( "/WEB-INF/views/biz/my_message.jsp" ).forward(req,resp); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <meta charset= "UTF-8" > <title>修改留言</title> <link rel= "stylesheet" href= "../../../css/bootstrap.min.css" > <link rel= "stylesheet" href= "../../../css/add.css" > </head> <body> <nav class = "navbar navbar-default" > <div class = "container" > <div class = "navbar-header" > <a class = "navbar-brand" href= "/message/list.do" > 慕课网留言板 </a> </div> </div> </nav> <div class = "container" > <div class = "jumbotron" > <h1>Hello, ${user.username}!</h1> <p>既然来了,就说点什么吧</p> </div> <div class = "page-header" > <h3><small>修改留言</small></h3> </div> <form class = "form-horizontal" action= "/update/message.do" method= "post" > <div class = "form-group" > <label for = "inputTitle" class = "col-sm-2 control-label" >标题 :</label> <div class = "col-sm-8" > <input name= "title" class = "form-control" id= "inputTitle" placeholder= "title" value= "${message.title}" > </div> </div> <div class = "form-group" > <label for = "inputContent" class = "col-sm-2 control-label" >内容 :</label> <div class = "col-sm-8" > <textarea name= "content" class = "form-control" rows= "3" id= "inputContent" placeholder= "Content" >${message.content}</textarea> </div> </div> <div class = "form-group" > <div class = "col-sm-offset-2 col-sm-10" > <button type= "submit" class = "btn btn-primary" >修改</button> </div> </div> <div class = "form-group" > <div class = "col-sm-offset-2 col-sm-10" > <a class = "btn btn-default" href= "/message/list.do" >返回</a> </div> </div> </form> </div> <footer class = "text-center" > copy @imooc </footer> </body> </html> |
1 2 3 4 5 6 7 8 9 | //修改留言的方法 public boolean update(Message message){ return messageDAO.update(message); } //通过id在数据库中查询留言 public Message getMessageById(String id){ return messageDAO.getMessageById(id); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /** * 通过id在数据库中查询留言 */ public Message getMessageById(String id){ Message message = null ; Connection conn = null ; PreparedStatement pstmt = null ; ResultSet rs = null ; try { conn = JDBCUtils.getConnection(); String sql = "select title,content from message where id = ?" ; pstmt = conn.prepareStatement(sql); pstmt.setString( 1 ,id); rs = pstmt.executeQuery(); while (rs.next()){ message = new Message(); message.setTitle(rs.getString( "title" )); message.setContent(rs.getString( "content" )); } } catch (Exception e){ e.printStackTrace(); } finally { JDBCUtils.release(conn,pstmt,rs); } return message; } /** * 修改留言的方法 */ public boolean update(Message message){ Connection conn = null ; PreparedStatement pstmt = null ; try { conn = JDBCUtils.getConnection(); String sql = "update message set title = ?,content = ? where id = ?" ; pstmt = conn.prepareStatement(sql); pstmt.setString( 1 ,message.getTitle()); pstmt.setString( 2 ,message.getContent()); pstmt.setString( 3 ,message.getId()); pstmt.executeUpdate(); } catch (Exception e){ e.printStackTrace(); return false ; } finally { JDBCUtils.release(conn,pstmt); } return true ; } |
编辑好修改内容点击保存之后无法完成修改,数据库中也没有修改
1
收起
正在回答 回答被采纳积分+1
3回答
chrismorgen
2018-12-12 16:16:58
1、建议你在浏览器的控制台中按F12,看一下浏览器控制台中有没有报错提示。
2、建议你在修改留言的方法中打印message对象试试,看看里面时候有前端页面传过来的信息。祝学习愉快~
精慕门6573819
2018-12-12 14:21:37
chrismorgen
2018-12-12 14:12:49
你好同学,建议你看一下下图标记的两个窗口中有没有报错信息,如果有,建议将报错信息粘贴一下,祝学习愉快~
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧