Java jqgrid如何显示服务器端消息

Java jqgrid如何显示服务器端消息,java,javascript,servlets,jqgrid,response,Java,Javascript,Servlets,Jqgrid,Response,我使用jqGrid以表格格式显示数据,使用JSP和servlet 编辑 我想在执行插入、更新、删除等操作时显示来自服务器的错误(数据类型:“xml”) JQGrid jQuery("#list10_d").jqGrid({ height:250, width:600, url:'Assignment?action=Assign', datatype: "xml",

我使用
jqGrid
以表格格式显示数据,使用
JSP
servlet

编辑

我想在执行插入、更新、删除等操作时显示来自服务器的错误<代码>(数据类型:“xml”)

JQGrid

jQuery("#list10_d").jqGrid({
                height:250,
                width:600,
                url:'Assignment?action=Assign',
                datatype: "xml",
                colNames:['Sr. No.','PID',  'DATE',  'EMPID'],
                colModel:[{name:'srNo',index:'srNo', width:30,sortable:false},
                           {name:'PID',index:'PID',width:0, sortable:true,editable:false},
                           {name:'DATE',index:'DATE', width:75,sortable:true,editable:true,editoptions: { dataInit: function(el) { setTimeout(function() { $(el).datepicker({dateFormat:"dd-M-yy",showButtonPanel: true,changeYear: true,changeMonth: true}).attr('readonly','readonly'); }, 200); }}},
                           {name:'EMPID',index:'EMPID', width:150,sortable:true,editable:true}
                           ],
                rowNum:10,
                rowList:[10,20,50,100],
                pager: '#pager10_d',
                sortname: 'PID',
                viewrecords: true,
                sortorder: "asc",

                    },
                multiselect: true,
                editurl: "Assignment?action=Edit",
                caption:"Assignment"
            } ).navGrid('#pager10_d',{edit:false,add:true,del:false,addtext:'Assign '},
                    {},
                    {modal:true,jqModal: false,closeOnEscape:true,savekey: [true,13],closeOnEscape:true, recreateForm: true,width:500,mtype:'POST', url: 'Assignment',editData:{action: 'Assign',PID: function () {return PID;}}, 
                afterSubmit: function (response) {
                        alert('After Submit \n' +'statusText: '+ response.statusText);
                        var myInfo = '<div class="ui-state-highlight ui-corner-all">'+
                                     '<span class="ui-icon ui-icon-info" ' +
                                         'style="float: left; margin-right: .3em;"></span>' +
                                     response.statusText + 'Inserted'+
                                     '</div>',
                             $infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),
                            $infoTd = $infoTr.children("td.topinfo"); 
                        $infoTd.html(myInfo);
                        $infoTr.show();

                        // display status message to 3 sec only
                        setTimeout(function () {
                            $infoTr.slideUp("slow");
                        }, 5000);

                        return [true, "", ""]; // response should be interpreted as successful
                    },
                    errorTextFormat: function (response) {
                    alert('Error Text Format: \n' +'statusText: '+ response.statusText);

                        return '<span class="ui-icon ui-icon-alert" ' +
                                     'style="float:left; margin-right:.3em;"></span>' +
                                    response.statusText;},
                    {closeOnEscape:true, recreateForm: true,mtype: 'POST',url: 'Assignment',delData: {action: 'Delete',PID: function () {return PID;}}},
                    {}) ;
例如上面的例子

  • 从jqgrid插入
    记录后,
    中不显示任何消息
    网格如果记录
    插入成功
  • 错误状态:“未授权”。错误代码:若servlet未能在数据库中插入记录,则显示401
我的问题是:

  • 在从jqgrid插入记录之后,如果插入了记录,那么我应该如何向用户显示插入数据的信息
  • 在插入时,我应该如何向用户发出
    错误的消息(我应该使用哪个
    错误代码

提前感谢….

我在对我的服务器的编辑调用中做了一些类似的事情,因此我认为这将以非常类似于添加的方式工作

在控制器上进行编辑/删除/添加调用后,您将确定是否有消息要传递给用户,如果有,则通过JSON(在您的示例中为XML)将其传递回网格

在jqGrid中,您将拥有编辑/删除/添加功能:

    function EditCollectionItem (rowid, grid){
        $(grid).jqGrid('editGridRow', rowid,
        {
            viewPagerButtons: false,
            editData: { },
            afterComplete: function (response) {
                var DialogVars = $.parseJSON(response.responseText); //parse the string that was returned in responseText into an object
                //if there was a failure with the update, or there was information to pass to the user
                if (!DialogVars.success || DialogVars.showMessage) {
                    alert(DialogVars.message);
                }
            } //afterComplete
        }); //$(grid).jqGrid('editGridRow
    }//function EditCollectionItem (rowid, grid){
因此,在上面的示例中,如果操作失败,您可以显示带有
success=false
的消息,或者如果操作已完成,但您希望将一些额外信息传递给用户,您也可以使用
suces=true
&
showMessage=true

这是一个JSON编辑示例,但XML和添加/删除操作的概念和逻辑应该相同

我建议in和in使用现有的网格表单隐藏行(
tr.tinfo
)来显示没有错误的信息。因为答案并不广为人知,所以我在这里重复相同的信息,但我将尝试更详细地解释

首先,了解哪些元素具有标准的编辑/添加表单非常重要。使用IE或Chrome、Firebug或许多其他工具的开发人员工具,您很容易发现jqGrid创建的添加/编辑表单在表单顶部包含两个隐藏行:

默认情况下,第一行将用作错误消息的位置。可以使用
errorTextFormat
稍微定制一下信息

如果服务器响应包含错误HTTP状态代码(>=400),则将调用回调
errorTextFormat
,您可以使用

errorTextFormat:函数(响应){
返回response.responseText;
}
或者类似的

errorTextFormat:函数(响应){
返回“”+
response.responseText;
}
显示错误消息,如

同样,如果服务器响应包含成功,则可以使用
afterSubmit
回调在提交编辑/添加的数据后显示状态消息。
afterSubmit
的实现可能与以下内容有关

afterSubmit: function (response) {
    var myInfo = '<div class="ui-state-highlight ui-corner-all">'+
                 '<span class="ui-icon ui-icon-info" ' +
                     'style="float: left; margin-right: .3em;"></span>' +
                 response.responseText +
                 '</div>',
        $infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),
        $infoTd = $infoTr.children("td.topinfo");
    $infoTd.html(myInfo);
    $infoTr.show();

    // display status message to 3 sec only
    setTimeout(function () {
        $infoTr.slideUp("slow");
    }, 3000);

    return [true, "", ""]; // response should be interpreted as successful
}
afterSubmit:函数(响应){
var myInfo=''+
'' +
response.responseText+
'',
$infoTr=$(“#TblGrid_389;”+$.jgrid.jqID(this.id)+>tbody>tr.tinfo”),
$infoTd=$infoTr.children(“td.topinfo”);
$infoTd.html(myInfo);
$infoTr.show();
//仅显示状态信息至3秒
setTimeout(函数(){
$infoTr.slideUp(“慢”);
}, 3000);
return[true,“,”];//响应应解释为成功
}
代码将仅显示状态消息3秒,然后使用动画将其隐藏。看起来像


我希望这是您所需要的。

我已经更新了我的问题,在实现了您的代码后,现在消息显示在表单顶部,如图所示,但只显示状态代码,如插入成功时只显示
OK
,插入失败时只显示
Not Found
,但我无法以表单的形式显示servlet发送的
自定义
消息。。将错误从servlet发送到网格时是否出错?任何帮助都将不胜感激。@Bhushan:也许你以前没有读过。你能将我的答案的当前代码与youth one进行比较吗?我使用了
response.responseText
,但它显示nothing@Bhushan:您应该验证来自服务器HTTP响应的信息。您可以使用Fiddler、Firebug或IE(按F12键启动)或Google Chrome的开发工具来检查完整的HTTP流量。您还可以在
afterSubmit
中设置断点,并检查
response
@Bhushan的属性:您是否修复了服务器代码?我不是Java开发人员,但我认为使用
response.setStatus
(比如
response.setStatus(200,“插入成功”);
)的第二个参数是错误的。类似于
PrintWriter out=response.getWriter()的代码;输出。打印(“插入成功”);out.flush()似乎对我更正确。可能您应该使用
response.setContentType(“text/html;charset=UTF-8”)
而不是
response.setContentType(“text/xml”)
。看见
    function EditCollectionItem (rowid, grid){
        $(grid).jqGrid('editGridRow', rowid,
        {
            viewPagerButtons: false,
            editData: { },
            afterComplete: function (response) {
                var DialogVars = $.parseJSON(response.responseText); //parse the string that was returned in responseText into an object
                //if there was a failure with the update, or there was information to pass to the user
                if (!DialogVars.success || DialogVars.showMessage) {
                    alert(DialogVars.message);
                }
            } //afterComplete
        }); //$(grid).jqGrid('editGridRow
    }//function EditCollectionItem (rowid, grid){
afterSubmit: function (response) {
    var myInfo = '<div class="ui-state-highlight ui-corner-all">'+
                 '<span class="ui-icon ui-icon-info" ' +
                     'style="float: left; margin-right: .3em;"></span>' +
                 response.responseText +
                 '</div>',
        $infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),
        $infoTd = $infoTr.children("td.topinfo");
    $infoTd.html(myInfo);
    $infoTr.show();

    // display status message to 3 sec only
    setTimeout(function () {
        $infoTr.slideUp("slow");
    }, 3000);

    return [true, "", ""]; // response should be interpreted as successful
}