Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an arbitrary URL in new window using a PrimeFaces button

Tags:

jsf

primefaces

I have the below output link which does its job:

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

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

However, I'd 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 bean property. How can I achieve this anyway?

like image 330
Alexander Villalobos Avatar asked Jun 03 '15 20:06

Alexander Villalobos


1 Answers

The <p:commandButton> basically submits a POST request to the URL as specified by its parent <h:form>, which defaults indeed to the current request URL (you know, "postback"). The action attribute basically invokes a bean method and uses the returned value as navigation case outcome. An URL does not necessarily represent a sensible navigation case outcome.

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 a <p:commandButton>, but that's unnecessarily overcomplicated.

like image 149
BalusC Avatar answered Nov 15 '22 08:11

BalusC