同步javascript调用阻止IHM直到进程完成

时间:2016-02-01 14:24:49

标签: javascript jquery css struts2

首先,我要说的是我要做的是点击一个按钮(这将导出一个excel,所以这个动作的类型是一个流),而服务器正在处理我想阻止IHM使用将显示GIF的叠加层。

所以为了做到这一点,我使用了方法" css" jquery设置"显示"阻止(激活覆盖)和无(阻止它)。 问题是,这似乎不起作用,因为它激活并立即停用叠加(我在chrome上调试模式验证,如果我在.css之后停止IHM('显示', '阻止')它将使用已结算的背景+ gif冻结IHM,但如果我没有设置断点,它将绕过它,调用动作(使用提交)并删除背景+ popup)这显然是异步完成的。 我尝试以这种Ajax调用同步方式执行此操作,但在显示返回字符串结果(服务器端)后,页面感觉错误(我认为因为它是流结果):

FALSE

这是(在JSP中)执行此操作的代码:

function exportComplexExcel() {  
    // export excel
        jQuery("#overlayBackground").css('display', 'block'); // displaying the overlay
        jQuery("#overlayPopup").css('display', 'block'); // displaying the popup
        $.ajax({
                    method : "GET",
                    url : "exportExcelMdt.action",
                    traditional : true,
                    async : false,
                    contentType : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    complete : function(data) {
                        jQuery("#overlayBackground").css('display', 'none'); // displaying the overlay
                        jQuery("#overlayPopup").css('display', 'none'); // displaying the popup
                    }
                });
}

以下是使用方法exportExcel():

function exportComplexExcel() {  
    jQuery("#overlayBackground").css('display', 'block'); // displaying the overlay
    jQuery("#overlayPopup").css('display', 'block'); // displaying the popup

    $("#searchForm").attr("action", "complexExportExcelMdt.action");
    $("#searchForm").submit();

    jQuery("#overlayBackground").css('display', 'none'); // displaying the overlay
    jQuery("#overlayPopup").css('display', 'none'); // displaying the popup
}

我知道定义onClick动作的代码很奇怪,但它就是它(意思是旧代码:D)方法" insertExcelButtonForExtraction"在另一个函数中被调用,该函数用于" onblur" " s中的字段:textfield" (我认为我们不需要看到它)。

这是css文件(或此处调用的部分):

function insertComplexExcelButtonForExtraction() {
    // creer le nom depuis "#gview_" + l'id de la grid  
    var $btnComplexExcel = $('#gview_resultGrid > div > #btnComplexExcel');

    // Check if object already exists 
    if(!$btnComplexExcel.length) {
        var $a = $('#gview_resultGrid > div > #btnExcel');
        var $newA = $a.clone();

        $newA.attr('style', 'right: 50px; padding-top: 2px;');
        $newA.attr('id', 'btnComplexExcel');
        $newA.children("#searchForm_exportExcelMdt").remove();
        $newA.append($('<input id="searchForm_complexExportExcelMdt" type="image" onclick="exportComplexExcel();" src="/myApp/img/page_white_excel_plus.png" title="<s:text name="myApp.message.js.button.complexExcelExtraction"/>"/>')); 
        $newA.insertAfter($a);
    }
}

这是struts动作:

我将此代码用于我的操作方法:

#overlayBackground {
background: #000000;
opacity:0.5;
bottom: 0;
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 10000;
}

#overlayPopup {
background: none repeat scroll 0 0 #FFFFFF;
border: 20px solid #DDDDDD;
left: 31%;
padding: 50px;
position: fixed;
text-align: center;
top: 28%;
width: 380px;
z-index: 20000;
-moz-border-radius:30px 0;
}

以下是相关的struts.xml文件:

@Action(value = "exportExcelMdt", results = {

        @Result(name = "exportExcel", 
                type = "stream", 
              params = { "inputName", "inputStream", 
                       "contentType", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
                "contentDisposition", "attachment;filename=${filename};", 
                        "bufferSize", "1024" }),

        @Result(name = "success", 
                type = ActionSupport.NONE) 
})

public String doExportExcel() {

    Boolean exportPending = (Boolean) this.session.get(EXPORT_EXCEL);
    if (exportPending == null) { // this is used as a semaphore to prevent the user from re-calling the export action (server-side of course)
        try {
            this.session.put(EXPORT_EXCEL, Boolean.TRUE);
            logger.debug("export excel...");
            this.filename = this.getText("myApp.message.mandat.recherche.extraction.filename");
            this.inputStream = this.extractExcel(this.searchResults(false)); // this will just get the related beans and write the excel file using Apache POI
            return "exportExcel";
        } catch (Exception e) {
            logger.debug("export excel erreur...");
        } finally {
            this.session.remove(EXPORT_EXCEL);
        }
    } else {
        //          this.addActionMessage("An export excel is already running !");
    }

    return SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

嗯,输出流应该由结果返回,但是使用此注释将操作映射到方法doExportExcel

@Action(value = "exportExcelMdt", results = {

        @Result(name = "exportExcel", 
                type = "stream", 
              params = { "inputName", "inputStream", 
                       "contentType", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
                "contentDisposition", "attachment;filename=${filename};", 
                        "bufferSize", "1024" }),

        @Result(name = "success", 
                type = ActionSupport.NONE) 
})

操作可以返回两个结果,包括全局结果,但实现此操作的方法的逻辑表明,在某些情况下,它应返回SUCCESS结果。但是这个结果有错误的类型属性值。 Struts2会混淆执行此结果,因为它未定义或未继承。如果您需要返回NONE结果,则可以在操作中执行此操作,而不会使用此结果覆盖操作配置。只需返回NONE,因为它是全局结果。