<% Comment c=(Comment)request.getAttribute("comment");%>
<h2>修改评论</h2>    
<form id="form1" name="form1" method="post" action="/blog/servlet/CommentServlet">
<input type="hidden" name="id" value="<%=c.getId() %>" />得到的<%=c.getId() %>值是null,在javabean Comment中有getId方法
而<%=c.getUsername()%>能得到值,也就是说只有调用getId()时,返回值为null,不知道为什么?

解决方案 »

  1.   

    下面是editComment.jsp
    <%@ page language="java" contentType="text/html;charset=UTF-8"%><%@ page import="cn.com.jobedu.blog.Comment"%>
    <%@ page import="java.util.List"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <% Comment c=(Comment)request.getAttribute("comment");%><h2>修改评论</h2>    
    <form id="form1" name="form1" method="post" action="/blog/servlet/CommentServlet"><input type="hidden" name="id" value="<%=c.getId() %>" /><input type="hidden" name="method" value="update" />
    <table id="tab">
    <tr>
        <td>评论人: </td>
        <td>
    <input name="username" type="text" id="title" size="50"   value="<%=c.getUsername()%>"/>
    </td>
    </tr>
    <tr>
        <td colspan="2">内容: <br/>
             <textarea name="content" cols="60" rows="18" id="content"><%=c.getContent()%></textarea>
        </td>
    </tr><tr>
        <td colspan="2">
    <input type="submit" name="submit" value="更新"/>
        </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    下面是javabean Comment类
    package cn.com.jobedu.blog;import java.util.Date;public class Comment {
    private Integer id;
    private String username;
    private String content;
    private Date createdTime;
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }

    public Date getCreatedTime() {
    return createdTime;
    }
    public void setCreatedTime(Date createdTime) {
    this.createdTime = createdTime;
    }


    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;
    }


    }下面是更新,提交到servlet的update方法
    public void update(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String id = request.getParameter("id");
    String username = request.getParameter("username");
    String content = request.getParameter("content"); String sql = "update comment set username=?,content=? where id=?";
    String params[] = { username, content, id };
    QueryRunner qr = DbHelper.getQueryRunner(); try {
    qr.update(sql, params);
    } catch (SQLException e) {
    e.printStackTrace();
    }
     list(request, response);
    }

      

  2.   

    Comment c=(Comment)request.getAttribute("comment");%
    中的"comment"你是如何传过来的啊!可以看看代码不,估计你没有传对吧
      

  3.   

    public void preEdit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String id = request.getParameter("id");
    String sql = "select username,content from comment where id=" + id;
    QueryRunner qr = DbHelper.getQueryRunner();
    List list = null;
    Comment comment = null;
    try {
    list = (List) qr.query(sql, new BeanListHandler(Comment.class));
    comment = (Comment) list.get(0);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    request.setAttribute("comment", comment);
    request.getRequestDispatcher("/editComment.jsp").forward(request,
    response); }
      

  4.   

    comment应该传的对吧,否则在editComment.jsp中,不可能得到<%=c.getUsername()%>的值,这个值是能够输出的
      

  5.   

    String sql = "select username,content from comment where id=" + id;
    request.setAttribute("comment", comment);
    存进去的是通过查询后的结果,执行这个查询的时候,你并没有查询id这一列,当你在页面上取的时候肯定是null呀!