1,先上servlet的跳转代码:
if (Objects.equals(req.getServletPath(), "/editMessage.do")) { String msgId = req.getParameter("msgId"); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; List list = new ArrayList(); Map map = new HashMap(); try { con = JDBCUtil.getConnection(); String sql = "SELECT u.username,m.title,m.content" + " FROM user AS u" + " INNER JOIN message AS m" + " ON u.id=m.userId" + " WHERE m.id=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1,msgId); rs = pstmt.executeQuery(); if (rs.next()) { map.put("username", rs.getString("username")); map.put("title", rs.getString("title")); map.put("content", rs.getString("content")); list.add(map); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(rs, pstmt, con); req.setAttribute("messages", list); req.getRequestDispatcher("/WEB-INF/views/biz/edit_message.jsp").forward(req, resp); } } //end if Objects.equals editMessage.do
2,再上jsp的代码
<%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% List list = (List) request.getAttribute("messages"); System.out.println(list); %> <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"> <c:forEach items="${messages}" var="msg"> <p>${msg.username}</p> <p>${msg.title}</p> <p>${msg.content}</p> </c:forEach> <c:forEach items="${messages}" var="msg"> <div class="jumbotron"> <h1>Hello, ${msg.username}!</h1> <p>既然来了,就说点什么吧</p> </div> <div class="page-header"> <h3> <small>新建留言</small> </h3> </div> <form class="form-horizontal" action="/addMessage.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="${msg.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">${msg.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> </c:forEach> </div> <footer class="text-center"> copy@imooc </footer> </body> </html>
无论如何修改,都没有获取到值。
网上查询了很多办法:
解答一:检查字母大小写
我的没有问题
解答二:加上作用域
照样获取不到值
解答三:items和var的顺序不能反。好像有这么个解答,但是二者顺序不用讲究
解答四:在jsp里获取值并打印出来,看看是否能获取到值
我的真的获取到了,我最后真的不知道哪里出问题了。
于是,就找啊找,还真找到了,这个答案还真是被自己忽略了。
终极解答:
要导入标签:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
嗯,是的。就是这个标签的锅。其实是自己学的还不够好,哈哈。
希望能帮助到大家。
大家加油!