3

Is it possible to open a resource into a new browser Tab (like target="_newtab" for command buttons) from server side jsf code?

The following Code opens the resource in the same tab:

FacesContext.getCurrentInstance().getExternalContext().redirect("resource.jsp"); 

I'm using primefaces. I think there is a possibility with javascript and icefaces.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Ziagl
  • 492
  • 2
  • 8
  • 22

3 Answers3

6

You cannot control that in the server side. You need to control that in the client side. For example, the command button invoking the bean's action should have a target="_blank" on its parent <h:form>.

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

Or if you aren't doing any postprocessing stuff in the action method, just replace that button altogether by a plain link.

<a href="resource.jsp" target="_blank">link</a>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
5

The best solution that I found was:

PrimeFaces.current().executeScript("window.open('resource.jsp', '_newtab')");

Or when you're not on PrimeFaces 6.2 or newer yet:

RequestContext.getCurrentInstance().execute("window.open('resource.jsp', '_newtab')");
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ziagl
  • 492
  • 2
  • 8
  • 22
  • Not sure how this can be correct. I'm using PrimeFaces and there's no "execute" method available from the RequestContext current instance! Is there code missing here? – Brian Knoblauch May 25 '12 at 15:18
  • I guess you are using the wrong `RequestContext`. Did you import `org.primefaces.context.RequestContext`? – Manuel Feb 18 '13 at 13:12
  • Thank you so much, this was is useful for me, so if you use primefaces you can [Execute script](https://stackoverflow.com/questions/5675017/calling-a-javascript-function-from-managed-bean) and call window.open script `org.primefaces.PrimeFacescurrent().executeScript`. Maybe this be help to somebody. Regards. – Dagon Feb 16 '21 at 02:26
0

This worked for me on Icefaces:

JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "window.open(dir, '_newtab');");

where dir is the direction of the new tab.

jerry
  • 2,581
  • 1
  • 21
  • 32