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.
Luiggi has answered the POST matter. If the report is however available by a GET request, then there are other ways.
Use
window.open()
inoncomplete
.<h:form> ... <p:commandButton action="#{bean.submit}" update="@form" oncomplete="if (args && !args.validationFailed) window.open('reportURL')" /> </h:form>
Or conditionally render a
<h:outputScript>
withwindow.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()
withwindow.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}')
.
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 && !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