Mark Vincent Osea
Mark Vincent Osea

Reputation: 689

How to open multiple url/link in separate windows at once using JSF?

I want to open multiple pages of my JSF application using a command link in primefaces. Is there a way to do this using a backing bean? I need to open them at the same time because I need to display the pages to different monitors. Each pages has a <p:poll /> tag with the same interval and my goal is to make the UI look like updating at the same time.

<p:link outcome="/admin/page1">
      <span class="ui-button-text">page1</span>
</p:link>
<p:link outcome="/admin/page2">
      <span class="ui-button-text">page2</span>
</p:link>
<p:commandButton actionListener="#{someBean.openLinks}" />

Upvotes: 0

Views: 621

Answers (2)

Jasper de Vries
Jasper de Vries

Reputation: 20188

I would solve this on the client side using JavaScript to "click" the links when the button's action is completed. Advantages are you can use the button to process what's needed and also your links will be processed as they normally would if a user clicked on them. To open links in a new window add target to them (it accepts all values a normal plain html a href accepts). So, I would suggest:

<p:link id="myLink1" outcome="/admin/page1" target="target1">
      <span class="ui-button-text">page1</span>
</p:link>
<p:link id="myLink2" outcome="/admin/page2" target="target2">
      <span class="ui-button-text">page2</span>
</p:link>
<p:commandButton action="#{someBean.yourAction}"
                 oncomplete="openLinks()" />

You should add a JavaScript function openLinks:

function openLinks(){
    document.getElementById("myForm:myLink1").click();
    document.getElementById("myForm:myLink2").click();
}

I don't know what naming containers you are using, just use your browser's debug tools to inspect the link and find the actual id. If you have anything like j_idtXxx in it, make sure to set an id to the corresponding naming container.

Keep in mind that JSF always generates HTML (with JavaScript and CSS) for the client-side. So many you can apply the same plain html solutions to them (the openLinks JavaScript function would be identical for a plain html solution without any JSF).

Upvotes: 1

Bilal Dekar
Bilal Dekar

Reputation: 3976

this command line allows you to open a link from a managed bean :

RequestContext.getCurrentInstance().execute("window.open('" + url + "')");

Upvotes: 1

Related Questions