解決jqGrid新增或編輯記錄保存成功但提示錯誤的問題

在上一篇文章《》中,咱們詳細說明了一下如何建立一個能夠使用增刪改操做的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

相關文章
相關標籤/搜索