How to use target=“_blank” to open report in new window only if validation has not failed

吃可爱长大的小学妹 提交于 2019-12-18 05:04:35

问题


I have a report that needs to be opened in another tab, but only if the validation hasn't failed. In my case, the validation fails and the same page opens in another tab with the validations errors.


回答1:


Luiggi has answered the POST matter. If the report is however available by a GET request, then there are other ways.

  1. Use window.open() in oncomplete.

    <h:form>
        ...
        <p:commandButton action="#{bean.submit}" update="@form" 
            oncomplete="if (args &amp;&amp; !args.validationFailed) window.open('reportURL')" />
    </h:form>
    
  2. Or conditionally render a <h:outputScript> with window.open().

    <h:form>
        ...
        <p:commandButton action="#{bean.submit}" update="@form" />
        <h:outputScript rendered="#{facesContext.postback and not facesContext.validationFailed}">
            window.open('reportURL');
        </h:outputScript>
    </h:form>
    
  3. Or use PrimeFaces RequestContext#execute() with window.open().

    public void submit() { 
        // ...
        RequestContext.getCurrentInstance().execute("window.open('reportURL');");
    }
    

The first way requires the URL to be already definied before submit, the latter two ways allows you to use a dynamic URL like so window.open('#{bean.reportURL}').




回答2:


I don't know if this is a definitive answer, but you could have 2 for this: 1 that will trigger the server validation and other that will call the report in a new page. Here's a start code:

<h:form id="frmSomeReport">
    <!-- your fields with validations and stuff -->

    <!-- the 1st commandLink that will trigger the validations -->
    <!-- use the oncomplete JS method to trigger the 2nd link click -->
    <p:commandLink id="lnkShowReport" value="Show report"
        oncomplete="if (args &amp;&amp; !args.validationFailed) document.getElementById('frmSomeReport:lnkRealShowReport').click()" />

    <!-- the 2nd commandLink that will trigger the report in new window -->
    <!-- this commandLink is "non visible" for users -->
    <h:commandLink id="lnkRealShowReport" style="display:none"
        immediate="true" action="#{yourBean.yourAction}" target="_blank" />

</h:form>
<!-- a component to show the validation messages -->
<p:growl />

In order to check if the validation phase contained any error, check How to find indication of a Validation error (required=“true”) while doing ajax command



来源:https://stackoverflow.com/questions/14584187/how-to-use-target-blank-to-open-report-in-new-window-only-if-validation-has-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!