开发者

Open page in new browser tab from bean

开发者 https://www.devze.com 2023-03-31 06:49 出处:网络
Is it possible to open a resource into a new b开发者_运维知识库rowser Tab(like target=\"_newtab\" for command buttons) from server side jsf code?

Is it possible to open a resource into a new b开发者_运维知识库rowser 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.


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>


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')");


This worked for me on Icefaces:

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

where dir is the direction of the new tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号