Guilherme Abacherli
Guilherme Abacherli

Reputation: 335

JSF redirect/open multiple url from a List<String>

Need to open or redirect to more than one page after a <p:commandLink/> (of the primefaces library) is clicked.

There is a List that contains the urls. I've already tried:

    List<String> newUrlsList = returnNewUrlsList(oldUrl);

    for (int i = 0; i < newUrlsList.size(); i++) {
        //Executes the redirect for each of the elements in the list
        //In every url, in the case of the method returnNewUrlsList() has encountered more than one URL
        FacesContext.getCurrentInstance().getExternalContext().redirect(newUrlsList.get(i));
    }

But only the first URL is opened (i=0).


Besides that I also tried javascript as below:

        <a href="#" class="openPages"> Link </a>

Running:

        <script type="text/javascript">
            $('a.openPages').click(function (e) {
                e.preventDefault();
                window.open('http://www.google.com.br');
                window.open('http://www.google.com.br');
                window.open('http://www.google.com.br');
                window.open('http://www.google.com.br');
                window.open('http://www.google.com.br');
            });
        </script>

It works, but it is not the best method, because whenever it is necessary to open multiple tabs it displays the popup-blocked warning by the browser.


I would appreciate any good suggestion.

Thanks in advance!

Upvotes: 0

Views: 477

Answers (2)

Guilherme Abacherli
Guilherme Abacherli

Reputation: 335

As Oscar Pérez suggested it is not possible to open more than one tab from the managed bean (serverside). So we decided to display the pages that were found for the user so he can decide which one he wants to open.

So we put an dialog in the page that shows a list with the name of the pages:

<p:dialog appendTo="@(body)" header="Pages" id="urls" widgetVar="dlgUrls" modal="true" showEffect="fade" hideEffect="fade" resizable="false" draggable="false">
                <h:form id="formUrls">
                    <br/>
                    <p:outputLabel value="The system has encountered more than one page." style="font-size: 14px"/>
                    <br/>
                    <p:outputLabel value="Select which you wants to open:" style="font-size: 14px"/>
                    <br/>
                    <p:dataList id="dataListUrls" value="#{bean.urlsList}" var="url" type="ordered" style="font-size: 14px">
                        <p:commandLink value="#{url.pageName}" actionListener="#{bean.redirect(url.address)}" target="_blank" ajax="false"/>
                    </p:dataList>
                    <br/>
                    <br/>
                    <p:separator/>
                    <div align="center" style="background-color: #DEDEDE">
                        <p:commandButton value="Close" oncomplete="PF('dlgUrls').hide()" style="font-size: 14px; width: 100px"/>
                    </div>
                </h:form>
            </p:dialog>

Thanks for the help!

Upvotes: 0

Oscar P&#233;rez
Oscar P&#233;rez

Reputation: 4397

You can't open several windows from Primefaces (nor any other serverside engine). You must do it from a client side, as you already did with Javascript.

You can use an EL expression to build a dynamic URL list if needed, but you'll have to use Javascript's window.open() to have more than one page opened at the same time.

Upvotes: 2

Related Questions