09Q71AO534
09Q71AO534

Reputation: 4440

When i click on any link it should Open in the same New Window in JSF, Primefaces

In my JSF page i have few Links{link1,link2,link3,link4}-{Student Id's}.

What i tried is when i click on the Link it Opens the Student-Information in a "NEW WINDOW".

When i am clicking on the next Link it is Opening a New Window,but i want to open in the Same New Window i.e., the TARGET should be the Same NEW Window which is opened.

Diagramatical Representation:

Target Link to Same Window

My Piece of Code which i had tried from collecting things from STACKOVERFLOWOpen Page in New Window :

<p:dataTable id="studentDtTble" var="studData" value="#{studentController.dataList}">
        <p:columnGroup type="header">
            <p:row>
                    <p:column headerText="StudentId"></p:column>
                    <p:column headerText="StudentName"></p:column> 
                    <p:column headerText="Add" ></p:column>     
            </p:row>
        </p:columnGroup>
            <p:column>
                <p:commandLink id="sidlink" action="/studentinfo.xhtml" target="_blank">  
                    <h:outputText value="#{studData.studentId}" styleClass="txtlink" />
                </p:commandLink>
            </p:column>
            <p:column>
               <h:outputText value="#{studData.studentName}" />
            </p:column>
            <p:column >
                <p:selectBooleanCheckbox value="#{studData.add}" />
            </p:column>
    </p:dataTable>

Edited After @Nosnhoj Reply :When i Click on any of the SID Link then there Details Should Opened in the "Same NEW Window".

Upvotes: 4

Views: 15968

Answers (1)

nosnhoj
nosnhoj

Reputation: 793

Use <h:commandLink> instead of <p:commandLink>.

I tried your code and make a little change like this:

<h:commandLink id="sidlink" action="#{studentController.selectStudent(studData)}" target="_blank">  
    <h:outputText value="#{studData.studentId}" styleClass="txtlink" />
</h:commandLink>

I changed <p:commandLink> to <h:commandLink>, and call a method in the backing bean to set the selected Student.(For the New Window should need the student information.)

Here is the code of studentController.selectStudent :

private Student selectedStu;

public String selectStudent(Student stu) {
    selectedStu = stu;
    return "/studentinfo.xhtml";
}

And the following is the code of New Window

 <h:form>
    <h:commandLink value="Another Link" action="/anotherinfo.xhtml" target="_self"/>
    <br/>
    Student: <h:outputText value="#{studentController.selectedStu.studentName}"/>
 </h:form>

Which is just showing name of the selected Student, and another link you want.
Note that the target attribute is _self because you want the new page show in the same window.

Finally, the anotherinfo.xhtml is just an empty page I created via NetBeans, so there's no need to post it.

And here's what you may see:
enter image description here

After clicking the id of "Johnson" :
enter image description here

After clicking "Another Link" :
enter image description here


Update

As @User2561626 explain what he/she intended to, I update my answer as follow:
If you want a New Window pop up in stead of New Tab, you should change the code like:

<p:commandLink id="sidlink" actionListener="#{studentController.selectStudent(studData)}" oncomplete="window.open('studentinfo.xhtml', 'newwindow', 'width=300, height=250');">  
    <h:outputText value="#{studData.studentId}" styleClass="txtlink" />
</p:commandLink>

I change <h:commandLink> back to <p:commandLink>, make action as actionListener because we want to set the selected Student first, finally I add window.open in oncomplete attribute so that the new page will open in a new window but not tab.
Of course we need to edit the method studentController.selectStudent(), like this:

public void selectStudent(Student stu) {
    selectedStu = stu;
}

As you can see, the return type is void now, and the only thing this method do is just set the selected Student.
Hope this may help.

Upvotes: 5

Related Questions