调用javabean的getId方法返回null

sshajaxajax 2010-10-08 09:47:34
<% 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,不知道为什么?
...全文
334 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sshajaxajax 2010-10-08
  • 打赏
  • 举报
回复
谢谢啊,就是你说的那个问题,是我疏忽啦,呵呵,已经成功
Spring89 2010-10-08
  • 打赏
  • 举报
回复
String sql = "select username,content from comment where id=" + id;
request.setAttribute("comment", comment);
存进去的是通过查询后的结果,执行这个查询的时候,你并没有查询id这一列,当你在页面上取的时候肯定是null呀!
sshajaxajax 2010-10-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 huangjinjin520 的回复:]
Comment c=(Comment)request.getAttribute("comment");%
中的"comment"你是如何传过来的啊!可以看看代码不,估计你没有传对吧
[/Quote]
comment应该传的对吧,否则在editComment.jsp中,不可能得到<%=c.getUsername()%>的值,这个值是能够输出的
sshajaxajax 2010-10-08
  • 打赏
  • 举报
回复
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);

}
BUG弄潮儿 2010-10-08
  • 打赏
  • 举报
回复
Comment c=(Comment)request.getAttribute("comment");%
中的"comment"你是如何传过来的啊!可以看看代码不,估计你没有传对吧
Spring89 2010-10-08
  • 打赏
  • 举报
回复
这是修改呀,查询的时候呢?
sshajaxajax 2010-10-08
  • 打赏
  • 举报
回复
下面是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);
}



Spring89 2010-10-08
  • 打赏
  • 举报
回复
那得看你查询的时候是怎么做的,代码贴出来看看!

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧