如何通过Primefaces3.5使用JSF2.0验证并在成功的情况下打开一个新的选项卡

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

本文关键字:选项 一个 情况下 JSF2 使用 Primefaces3 验证 成功 何通过      更新时间:2023-09-26

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

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

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

我遇到的问题是:

1-我不能覆盖表单的目标属性,所以它在一个新的选项卡中打开。

2-我不能从javascript函数handleComplete中调用Bean方法runReport。我得到一个未定义的"click"属性ERROR。

感谢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

我终于解决了这个问题。

所用方法的说明。单击按钮提交表单后,调用ManagedBean中的验证函数,并使用RequestContext对象将回调参数(以及验证结果)添加到前端。

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

从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