在上一篇文章《》中,咱们详细说明了一下如何建立一个能够使用增删改操做的jqGrid。php
可是在实际的修改、新增保存中,会看到以下的错误提示:error Status:"OK".Error code: 900。实际上,修改(新增)的记录已经正确的保存到数据库中了。见下图:数据库
这是为何呢?一直为这个问题头痛苦恼了好几天。仔细阅读了jqGrid wiki上的文档,也google了许多的文章,仍然找不到相应的说明或者解决办法。json
后来研究了一下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; }
再次执行,发现保存成功以后,编辑窗口自动关闭,页面自动从新加载。很好!post
不过我一直在疑惑,到底服务器要怎么返回呢???仔细看了看jqGrid的Demo,想经过研究php文件来看看例子中是如何作的,也没有找到。而且,jqGrid Demo的编辑例子,是不实际向服务器提交修改内容的。说是为了安全缘由......this