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.
-
What validation? Server side or client side? – Quentin Jan 29 '13 at 13:35
-
@Quentin OP refers to server side validation, is part of JSF life cycle. – Luiggi Mendoza Jan 29 '13 at 13:37
-
Are you using any other framework like RichFaces or PrimeFaces? – Luiggi Mendoza Jan 29 '13 at 13:39
-
im using primefaces; tried both validations: server and client ('required=true'), tried 'form target="_blank with p:commandButton ajax=false' and 'h:commandLink target="_blank" ' – shouts Jan 29 '13 at 13:43
-
i tried use (!args.validationFailed) and a variable from bean (ajax="#{bean.variable}") in p:commandButton but didnt worked too – shouts Jan 29 '13 at 13:49
2 Answers
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
Primefaces#execute()
withwindow.open()
.public void submit() { // ... PrimeFaces.current().executeScript("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}')
.

- 1,082,665
- 372
- 3,610
- 3,555
-
I´m starting to learn JSF and PrimeFaces and wanted to know if there is some example this code? Because I have a doubt if this example can work in my case. I have a form that user enter two dates and submit this form to show records in a datatable in other form. But needed to be validate. – Diego Macario Nov 12 '13 at 15:53
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

- 1
- 1

- 85,076
- 16
- 154
- 332