如何使用带有 Primefaces 3.5 的 JSF 2.0 验证并成功打开新选项卡?

How to validate and if successful open a new tab using JSF 2.0 with Primefaces 3.5?

所以我已经阅读了几个解决方案,其中一些已经接近但还不够接近我需要实现的行为。

感觉应该很简单,也许确实如此,但我对 JSF 和 Primefaces 还很陌生,所以对我来说并不容易。

期望的行为:单击 'Run Report' 按钮后,首先验证是否成功执行在新选项卡中呈现报告的方法,否则保持在同一视图中并显示一些错误消息。 请参阅下面的代码。随时提出建议,指出错误,不良做法。反馈越多越好。我会很感激实际的答案而不是链接。我在 SO 上遇到了很多问题,但找不到可以解决我的问题的答案。

我遇到的问题是:

1- 我无法覆盖表单的目标属性,因此它会在新选项卡中打开。

2- 我无法从 javascript 函数 handleComplete 中调用 Bean 方法 runReport。我收到 'click' 属性 未定义错误。

感谢 Stack Overflow 的天才程序员!!!!!!

文件:report_input.xhmtl

      <h:form id="pieForm" target="_self">
        <p:growl  widgetVar="growlMsg" id="msgs" showDetail="true" sticky="true" />
        <p:panel header="Report Criteria">
        <h:panelGrid columns="5" columnClasses="col-top-style">
          <h:outputLabel value="Date Range:" />
          <h:outputLabel value="From:" />
          <h:outputLabel value="To:" />
          <p:calendar id="startDate" value="#{deficiencyBean.startDate}" mindate=""
       maxdate="#{deficiencyBean.todayDate}" readOnlyInputText="true" size="18" showButtonPanel="true"  popupIconOnly="true" mode="popup" showOn="button"  pattern="MM/dd/yyyy" />
          <p:calendar id="endDate" value="#{deficiencyBean.endDate}" mindate="" 
       maxdate="#{deficiencyBean.todayDate}" readOnlyInputText="true" size="18" showButtonPanel="true"  popupIconOnly="true" mode="popup" showOn="button" pattern="MM/dd/yyyy" />
        </h:panelGrid>
        <h:panelGrid id="submitButton" columns="3" columnClasses="col-top-style">
        <p:commandButton icon="ui-icon-check" update="msgs" action="#{deficiencyBean.runReport}" oncomplete="handleComplete(xhr, status, args) return false;" value="Run Report" />
        <h:commandButton id="realSubmit" actionListener="#{deficiencyBean.runReport}" style="display: none;"/>
        <p:commandButton id="validateFunction" actionListener="#{deficiencyBean.validate}" rendered="false" update="msgs"/> 
        <p:commandButton id="hiddenValidVar" value="#{deficiencyBean.validInput}"  oncomplete="handleComplete(xhr, status, args)"/>
       </h:panelGrid>
      </p:panel>
     </h:form>
    <script>
    function handleComplete(xhr,status,args){
        var valid = args.isValid;
        alert(valid);
        if(valid === 'true'){
        document.getElementById('pieForm').setAttribute("target", "_blank"); // NOT WORKING
        document.getElementById('pieForm').target="_blank";//NOT WORKING
        document.getElementById('pieForm:realSubmit').click();//NOT WORKING
        }
    }
     </script>
     </ui:composition>

    </html>

文件:ReportBean.java

@ManagedBean(name = "deficiencyBean")
@SessionScoped
@SuppressWarnings("serial")
public class deficiencyBean implements Serializable{
private Date startDate; //public getters n setters
private Date endDate;   //public getters n setters 
private Boolean validInput; //public getters n setters 
public Boolean validate(){
//SOME LOGIC
   RequestContext.getCurrentInstance().execute("document.getElementById('pieForm').target='_blank'");//NOT WORKING
    if(validInput){
        RequestContext.getCurrentInstance().addCallbackParam("isValid", validInput);

    }
}
public void runReport(){
   //JASPER REPORTS COMPILE, FILL REPORT and DISPLAY
}

}//END OF CLASS

终于解决问题了

所用方法的说明。 单击按钮提交表单后,调用托管 Bean 中的验证函数,并使用 RequestContext 对象向前端添加回调参数(带有验证结果)。

然后使用handleComplete(xhr, status, args)函数检查服务器验证的return值。

从 JavaScript 调用隐藏的 commandLink 提交报告,如果验证通过,然后 运行 报告并完成!!!!!

下次我会尽量简化我的问题。 如果您不清楚,请随时询问有关我的解决方案的问题。

更改如下:

文件:report_input.xhmtl

Beginning of the file ...
<h:panelGrid id="submitButton" columns="3" columnClasses="col-top-adhoc">
                <h:outputLabel></h:outputLabel>
                <p:commandButton icon="ui-icon-check" action="#{deficiencyBean.validate}" 
                update="msgs" value="Run Report" oncomplete="handleComplete(xhr, status, args)" />

                <h:commandLink action="#{deficiencyBean.runReport}" id="runReportLink" target="_blank" value="Run Report" style="display: none;">
                </h:commandLink>
            </h:panelGrid>
        </p:panel>
    </h:form>
    <script>
    function handleComplete(xhr,status,args){
        var valid = args.isValid;
        if(valid == true){          
            document.getElementById('pieForm:runReportLink').click();
        }
    }   
</script>

文件:ReportBean.java

public void validate(){
//SOME LOGIC
RequestContext reqContext = RequestContext.getCurrentInstance();
reqContext.addCallbackParam("isValid", validInput); 


}
public void runReport(){
   //JASPER REPORTS COMPILE, FILL REPORT and DISPLAY
}

}//END OF CLASS