2

Primefaces 3.5.14, Omnifaces 1.6

Is there a component in JSF/Prime that looks like a button, but that allows to open a new blank page with an external link ? So unlike a normal link, on click the new page URL must be initialy retained from server and just then new page must be opened. Like a commanButton with an action attribute, where a string result of an action method determine the new url.

Of course I can use p:button as in Styling one JSF external link to make it look like a button but in that case I must update link generated by p:button, depending on actions made on the page every time. This is not always desirable.

Community
  • 1
  • 1
Tony
  • 2,266
  • 4
  • 33
  • 54
  • I'm not sure how this question is different from http://stackoverflow.com/questions/18916605/how-to-make-link-to-external-url-in-jsf-that-looks-exactly-as-a-button. Are you concretely asking that you want to invoke a bean action method when the button is pressed and that the external URL is determined based on result of the actual action? – BalusC Sep 20 '13 at 14:42
  • @BalusC Yes that's right. Sorry for unclear formulating. – Tony Sep 20 '13 at 14:45

1 Answers1

8

You need target="_blank" on the parent form in order to submit into a new window and you need to invoke ExternalContext#redirect() (or OmniFaces' Faces#redirect()) in the action method in order to let the target window send a new GET request on the given URL. Importantly, when you use <p:commandButton> instead of <h:commandButton>, you also need to set ajax="false" because it doesn't respect target="_blank".

Thus, so:

<h:form target="_blank">
    <p:commandButton value="Submit" action="#{bean.submit}" ajax="false" />
</h:form>

with

public void submit() throws IOException {
    String redirectURL = determineItSomehow();
    Faces.redirect(redirectURL);
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your answer! Exactly what I needed. I used `form target="_blank"` on a form with others `commandButton`s which used ajax and it does not disturb. – Tony Sep 20 '13 at 15:19