我找视频教程写了留言本的程序,添加和查看是正确的,编辑是可以根据ID取到该留言信息,但是编辑后保存就报错,找半天不知道错哪里
这个是GuestbookAction里的代码
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorForm f = (DynaValidatorForm) form;
String id=request.getParameter("id");
Guestbook gb=manager.getGuestbook(id);
//执行这句是正确的,可以取到相关字段的值并显示在edit页面
                f.set("name", gb.getName());
f.set("email",gb.getEmail());
f.set("url", gb.getUrl());
f.set("title", gb.getTitle());
f.set("content", gb.getContent());
    request.setAttribute("guestbook.article.id", id);
return mapping.findForward("guestbook.edit");
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Guestbook gb=null;
DynaValidatorForm f = (DynaValidatorForm) form;
String id=request.getParameter("id");//可以输出ID,说明取到了ID
if(id==null||id==""){
gb=new Guestbook();
}else{
gb=manager.getGuestbook(id);
//错误好像在这里,但是和edit方法中调用的是同一函数啊
}
gb.setName((String)f.get("name"));
gb.setEmail((String)f.get("email"));
gb.setUrl((String)f.get("url"));
gb.setTitle((String)f.get("title"));
gb.setContent((String)f.get("content"));

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time=sdf.format(new Date());

gb.setTime(time);
manager.save(gb);

return list(mapping,form,request,response);
}

解决方案 »

  1.   

    GuestbookManagerImpl中的相关代码
        public Guestbook getGuestbook(String id) {
         return dao.getGuestbook(new Long(id));
            }
    GuestbookDaoHibernate中相关代码
        public Guestbook getGuestbook(Long id) {
         return (Guestbook)getHibernateTemplate().get("com.sprt.guestbook.model.Guestbook", id);
            }
      

  2.   

    edit页面
    <html:form action="/guestbook" onsubmit="return validateGuestbookForm(this)">
        <html:hidden property="id" value="${requestScope[guestbook.article.id]}"/>
        <html:hidden property="method" value="save"/>
            <tbody>
            <tr>
              <td bgcolor=#eefee0 colspan=2>&nbsp;<img src="images/gb-add.gif" 
                align="middle"> :::: 请 您 留 言 ::::::::::::::::<a href="<c:url value="/guestbook.do?method=list"/>">查看留言</a>
              </td>
            </tr>
            <tr>
              <td  width="35%">
                <p style="margin-top: 3px; margin-bottom: 3px">&nbsp;姓名:
                  <html:text property="name"/></p>
                <p style="margin-top: 3px; margin-bottom: 3px">&nbsp;主页:
                  <html:text property="url"/></p>
                <p style="margin-top: 3px; margin-bottom: 3px">&nbsp;主题:
                  <html:text property="title"/></p>
                <p style="margin-top: 3px; margin-bottom: 3px">&nbsp;email:
                  <html:text property="email"/>
                </p></td>
              <td width="65%" height=130>
                <p align=center><html:textarea property="content" rows="6" cols="40"/>
                <br>&nbsp;
                <input class=backc type=submit value="提 交" name="submit"> 
        <input class=backc type=reset value="重 填" name="reset">
                 </p>
               </td>
           </tr>
      

  3.   

    这个好改 你把 你强制转换的哪个对象 你打印出来看看是不是空值 是的话 你就修改下 jiuok了
      

  4.   

    java.lang.NumberFormatException: For input string: "" 这种情况一般都是强制格式转换时出现的错误
    比如说 转换int double时 要转换的值为字母拉 要转换值为空拉~都可能出现整个种问题
    你在代码段上多用几个sysout 看看哪个值输出的是"" 
    逐步判断下~就好了
      

  5.   

    就是那个对象取不出好像
    GuestbookManagerImpl中的相关代码 
        public Guestbook getGuestbook(String id) { 
        return dao.getGuestbook(new Long(id)); 
            } 
    是这里的么,可是edit方法中可以取到并显示到了编辑页面中
    public ActionForward edit(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) { 
    DynaValidatorForm f = (DynaValidatorForm) form; 
    String id=request.getParameter("id"); 
    Guestbook gb=manager.getGuestbook(id); 
    //执行这句是正确的,可以取到相关字段的值并显示在edit页面 
    public ActionForward save(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) { 
    Guestbook gb=null; 
    DynaValidatorForm f = (DynaValidatorForm) form; 
    String id=request.getParameter("id");//可以输出ID,说明取到了ID 
    if(id==null ¦ ¦id==""){ 
    gb=new Guestbook(); 
    }else{ 
    gb=manager.getGuestbook(id); 
    //错误好像在这里,但是和edit方法中调用的是同一函数啊 
      

  6.   

    我在action里面
    public ActionForward save(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    Guestbook gb=null;
    DynaValidatorForm f = (DynaValidatorForm) form;
    String id=request.getParameter("id");
    if(id==null||id==""){
    gb=new Guestbook();
    }else{
    gb=manager.getGuestbook(id);//在这之后就不执行了,所有我写输出也不执行
    }
                    System.out.println(f.get("name")); gb.setName((String)f.get("name"));
    gb.setEmail((String)f.get("email"));
    gb.setUrl((String)f.get("url"));
    gb.setTitle((String)f.get("title"));
    gb.setContent((String)f.get("content"));

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String time=sdf.format(new Date());

    gb.setTime(time);
    manager.save(gb);