2

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.

Ziagl
  • 472
  • 2
  • 8
  • 22

3 Answers3

5

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,040,783
  • 362
  • 3,548
  • 3,513
4

The best solution that I found was:

RequestContext context = RequestContext.getCurrentInstance();
context.execute("window.open('resource.jsp', '_newtab')");
Ziagl
  • 472
  • 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,585
  • 1
  • 20
  • 32