您的位置:首页 > 其它

解决jqGrid新增或编辑记录保存成功但提示错误的问题

2009-12-14 14:58 525 查看
在上一篇文章《》中,我们详细说明了一下如何创建一个可以使用增删改操作的jqGrid。

但是在实际的修改、新增保存中,会看到如下的错误提示:error Status:"OK".Error code: 900。实际上,修改(新增)的记录已经正确的保存到数据库中了。见下图:



这是为什么呢?一直为这个问题头痛苦恼了好几天。仔细阅读了jqGrid wiki上的文档,也google了许多的文章,仍然找不到相应的说明或者解决办法。

后来研究了一下jqGrid的源代码。src/grid.formedit.js文件,发现其中有一个函数postIt,其中有如下的代码:

if(Status != "success") {
ret[0] = false;
if ($.isFunction(rp_ge.errorTextFormat)) {
ret[1] = rp_ge.errorTextFormat(data);
} else {
ret[1] = Status + " Status: '" + data.statusText + "'. Error code: " + data.status;
}
} else {
// data is posted successful
// execute aftersubmit with the returned data from server
if( $.isFunction(rp_ge.afterSubmit) ) {
ret = rp_ge.afterSubmit(data,postdata);
}
}
看来问题是在这个地方。由此猜想jqGrid的增删改操作是要求服务器返回内容的。

我猜测,jqGrid要求服务器返回包含成功与否的status内容。所以我修改了一下Action的类方法的返回值:如下:

public String modifyBrand()
{
String result = "success";
try
{

MProductBrand mpb = new MProductBrand();
mpb.setBrandName(brandName);
mpb.setCode(code);
mpb.setStatus(status);
mpb.setLastModifiedDatetime(new Timestamp(System.currentTimeMillis()));

if(oper != null && oper.equals("edit")){ //编辑
mpb.setBrandId(new Integer(id));
this.brandService.modifyBrand(mpb);
}
else if (oper != null && oper.equals("add")){ //新增
MProductBrand mproductbrand1 = this.brandService.locateByBrandcode(mpb
.getCode().toString().trim().toUpperCase());
MProductBrand mproductbrand2 = this.brandService.locateByBrandName(mpb
.getBrandName().toString().trim());

if (mproductbrand1.getBrandId() == null && mproductbrand2.getBrandId() == null) //检查是否存在
{

this.brandService.addBrand(mpb);
}
else
{
log.warn("品牌代码或品牌名称已经存在");
result = "error";
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
log.warn("修改失败");
result = "error";
}

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json; charset=UTF-8");
try{
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
json.put("status", result);
out.print(json.toString());
}
catch(Exception e){
e.printStackTrace();
log.error("Error:Cannot create PrintWriter Object !");
}
return null;
}
再次执行,发现保存成功之后,编辑窗口自动关闭,页面自动重新加载。很好!

不过我一直在疑惑,到底服务器要怎么返回呢???仔细看了看jqGrid的Demo,想通过研究php文件来看看例子中是如何做的,也没有找到。并且,jqGrid Demo的编辑例子,是不实际向服务器提交修改内容的。说是为了安全原因......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐