我找视频教程写了留言本的程序,添加和查看是正确的,编辑是可以根据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);
}
这个是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);
}
解决方案 »
- 请各位大侠帮帮我吧,在学校机房可以运行的,在自己电脑之前可以,但现在我不知怎么了,下面是报错的只要运行action就不行了
- 有关Alexa排名的抓取代码
- 奇怪的jsp现象
- JSTL中对XML查询出错?大大们帮帮忙
- jsp中如果动态设置网页标题title
- hibernate如何映射多张表(急)
- JSP中变量取得Javascript函数返回值
- jsp+tomcat的入门问题,(也就是《JSP由浅入深(10)Beans and Forms处理中的例子提交网页报错》)
- 我到底错在哪儿了,各位大侠,请指点一下我这个小学徒!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 我正要学习前台界面的设计开发..有哪位大神可以告诉我一下这个里面的<s:include>啊什么的是干嘛的
- 问高手问题,请进来
- 请大家帮忙看一个简单的图片上传和显示的问题.谢谢大家.
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);
}
<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> <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"> 姓名:
<html:text property="name"/></p>
<p style="margin-top: 3px; margin-bottom: 3px"> 主页:
<html:text property="url"/></p>
<p style="margin-top: 3px; margin-bottom: 3px"> 主题:
<html:text property="title"/></p>
<p style="margin-top: 3px; margin-bottom: 3px"> email:
<html:text property="email"/>
</p></td>
<td width="65%" height=130>
<p align=center><html:textarea property="content" rows="6" cols="40"/>
<br>
<input class=backc type=submit value="提 交" name="submit">
<input class=backc type=reset value="重 填" name="reset">
</p>
</td>
</tr>
比如说 转换int double时 要转换的值为字母拉 要转换值为空拉~都可能出现整个种问题
你在代码段上多用几个sysout 看看哪个值输出的是""
逐步判断下~就好了
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方法中调用的是同一函数啊
}
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);