Javascript 在jsf中如何保持客户端请求对来自服务器的延迟响应保持活动状态

Javascript 在jsf中如何保持客户端请求对来自服务器的延迟响应保持活动状态,javascript,jquery,http,jsf,primefaces,Javascript,Jquery,Http,Jsf,Primefaces,在我的JSF页面中有一个p:commandButton。当用户单击时,将从数据库中检索大量数据,并将其写入excel文件,然后作为响应返回给用户。这可能需要几个小时。但在使用IE将近一小时后,它显示“无法加载页面”错误消息,即使进程仍在服务器端运行。我知道这是由于客户端对服务器的预期响应时间进行了配置。它因浏览器而异 即使很困难,在计算过程中,我不断地从服务器向客户机发送ajax更新,以向用户显示处理数据的百分比,这是可行的,但我的客户机还是死掉了。此外,在我的JSF页面上有一个p:progre

在我的JSF页面中有一个p:commandButton。当用户单击时,将从数据库中检索大量数据,并将其写入excel文件,然后作为响应返回给用户。这可能需要几个小时。但在使用IE将近一小时后,它显示“无法加载页面”错误消息,即使进程仍在服务器端运行。我知道这是由于客户端对服务器的预期响应时间进行了配置。它因浏览器而异

即使很困难,在计算过程中,我不断地从服务器向客户机发送ajax更新,以向用户显示处理数据的百分比,这是可行的,但我的客户机还是死掉了。此外,在我的JSF页面上有一个p:progressBar,它还显示了每次更新间隔的百分比

如何通过代码让我的客户保持活力?因此,在发送请求后,它可以等待响应很长时间

我们有不同的用户使用不同的浏览器。我需要一个解决方案,它适用于所有主要浏览器。这是我的密码

<p:dialog id="alert-exp-info-dialog"
          widgetVar="alertExpInfoDialog">

    <p:ajax event="close"
            listener="#{bean.onExportDialogClose}"
            process="@this"
            update=":alert-exp-info-form"
            immediate="true" />

    <h:form id="alert-exp-info-form">

        <h:outputFormat id="alert-exp-detail"
                        value="So info for user">
            <f:param value="#{bean.exportedAlertsCount}" />
            <f:param value="#{bean.totalExportableAlerts}" />
        </h:outputFormat>

        <p:progressBar id="alert-exp-progress" 
                        widgetVar="alertExpProgress"  
                        value="#{bean.exportProgress}" 
                        labelTemplate="{value}%"
                        interval="3000"
                        ajax="true">
                <p:ajax event="complete" 
                        listener="# {bean.onExportComplete}"/>
                <f:event type="preValidate" 
                        update="alert-exp-abort-btn" 
                        listener="#{bean.updateComponents}" />
        </p:progressBar>

        <h:panelGroup id="alert-exp-button-panel">
        <p:commandButton id="alert-exp-proceed-btn"
                        value="Process"
                        action="#{bean.doCalculation}"
                        onclick="alertExpProgress.start();"
                        ajax="false">
            <p:ajax update="alert-exp-button-panel" />
        </p:commandButton>
        </h:panelGroup>

    </h:form>

</p:dialog>

public void doCalculation()
{
    FacesContext context = FacesContext.getCurrentInstance();
    //Too long calculation...
    for ( loop )
    {
    ...
    ...
    }
    //Finally writing excel file on responce
    context.setResponseContentType( "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet" );
    context.setResponseHeader( "Expires", "0" );
    context.setResponseHeader( "Cache-Control", "must-revalidate, post-  check=0, pre-check=0" );
    context.setResponseHeader( "Pragma", "public" );
    context.setResponseHeader( "Content-disposition", "attachment;filename=" + ( fileName.endsWith( ".xlsx" ) ? fileName : fileName + ".xlsx" ) );
    context.addResponseCookie( Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>() );

    OutputStream out = context.getResponseOutputStream();
    generatedExcel.write( out );
    out.close();
    context.responseFlushBuffer();
    context.responseComplete();
}  

public void updateComponents()
{
    RequestContext.getCurrentInstance().update( "alert-exp-info-form:alert-    exp-detail" );
}

public void onExportComplete()
{
    RequestContext context = RequestContext.getCurrentInstance();
    context.execute("alertExpInfoDialog.hide();");
}

公开作废文件计算()
{
FacesContext context=FacesContext.getCurrentInstance();
//计算太长。。。
for(循环)
{
...
...
}
//最后在response上编写excel文件
setResponseContentType(“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”);
setResponseHeader(“过期”,“0”);
setResponseHeader(“缓存控制”,“必须重新验证,后检查=0,预检查=0”);
setResponseHeader(“Pragma”、“public”);
context.setResponseHeader(“内容处置”、“附件;文件名=“+(filename.endsWith(“.xlsx”)?filename:filename+”.xlsx”);
addResponseCookie(Constants.DOWNLOAD_COOKIE,“true”,新HashMap());
OutputStream out=context.getResponseOutputStream();
生成EXCEL。写入(输出);
out.close();
context.responseFlushBuffer();
context.responseComplete();
}  
public void updateComponents()
{
RequestContext.getCurrentInstance().update(“警报exp信息表单:警报-exp详细信息”);
}
public void onExportComplete()
{
RequestContext=RequestContext.getCurrentInstance();
execute(“alertExpInfoDialog.hide();”;
}

我通过ajax触发长时间处理解决了问题,最后通过单独的按钮单击下载生成的excel文件。

我通过ajax触发长时间处理解决了问题,最后通过单独的按钮单击下载生成的excel文件。

试图让请求保持活动状态,但时间不会太长真有道理。为什么您不能在流程完成后发出额外的请求以获取数据?@承办人希望客户对响应做什么?您所说的“持续更新客户端百分比”是什么意思?该描述是否与“以便在发送请求后,它可以等待很长时间以获得响应”相反?您是否尝试过使用
WebSocket
?编辑的问题。我正在编写excel文件,它在UI中显示为一个弹出窗口,用户可以从浏览器中保存或打开它。我是否应该使用onExportComplete()来编写响应文件?不,也不要使用ajax调用的oncomplete。使用ajax开始以一种火而忘却的方式生成文件。然后使用websockets以完成百分比更新客户端,当客户端收到100%的完成百分比时,您可以进行实际下载。加上1,建议您在ajax事件上执行此操作。现在唯一的问题是如何通过RequestContext下载文件?在我使用FacesContext编写button click上的file in action方法之前,试图让请求保持活动那么长时间是没有意义的。为什么您不能在流程完成后发出额外的请求以获取数据?@承办人希望客户对响应做什么?您所说的“持续更新客户端百分比”是什么意思?该描述是否与“以便在发送请求后,它可以等待很长时间以获得响应”相反?您是否尝试过使用
WebSocket
?编辑的问题。我正在编写excel文件,它在UI中显示为一个弹出窗口,用户可以从浏览器中保存或打开它。我是否应该使用onExportComplete()来编写响应文件?不,也不要使用ajax调用的oncomplete。使用ajax开始以一种火而忘却的方式生成文件。然后使用websockets以完成百分比更新客户端,当客户端收到100%的完成百分比时,您可以进行实际下载。加上1,建议您在ajax事件上执行此操作。现在唯一的问题是如何通过RequestContext下载文件?在我使用FacesContext编写按钮上的file in action方法之前。