How to open a custom URL in a new window using the PrimeFaces button

I have the following output link that does its job:

<h:outputLink value="#{verDocumentoController.url()}" target="_blank"> show document </h:outputLink> 

It opens the URL obtained as the bean property in a new window.

However, I would like to turn the link into a button in PrimeFaces look'n'feel. I tried as below:

 <p:commandButton value="show document" action="#{verDocumentoController.url()}" onclick="form.target='_blank'" ajax="false" /> 

But it only reopens the current page in a new window, and not the URL specified as a bean property. How can I achieve this?

+6
source share
1 answer

<p:commandButton> basically sends a POST request to the URL specified by its parent <h:form> , which by default really matches the current request URL (you know, "postback"). The action attribute basically calls the bean method and uses the return value as the result for navigation. The URL does not necessarily represent a reasonable result in case of navigation.

Just use window.open() instead on a simple <p:button> .

 <p:button value="show document" onclick="window.open('#{verDocumentoController.url()}');return false;" /> 

You can also do this on <p:commandButton> , but it's overly complicated.

+10
source

Source: https://habr.com/ru/post/988480/


All Articles