How to use target = "_ blank" to open a report in a new window, only if the verification fails

I have a report that needs to be opened in a different tab, but only if the verification has not completed. In my case, the verification fails, and the same page opens on another tab with verification errors.

+4
source share
2 answers

Luigi answered the POST question. If the report, however, is available at the request of GET, then there are other ways.

  • 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> 
  • Or conditionally draw a <h:outputScript> using 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> 
  • Or use PrimeFaces RequestContext#execute() with window.open() .

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

The first method requires that the URL has already been determined before sending, the last two methods allow you to use a dynamic URL, for example window.open('#{bean.reportURL}') .

+3
source

I do not know if this is the final answer, but you can have 2 for this: 1, which initiates a server check and others that will trigger a report on a new page. Here is the starting 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 /> 

To check if there was any error during the verification step, check How to find the indication of the verification error (required = "true") when executing the ajax command

+1
source

All Articles