【问题标题】:When i click on any link it should Open in the same New Window in JSF, Primefaces当我单击任何链接时,它应该在 JSF 的同一个新窗口中打开,Primefaces
【发布时间】:2014-01-18 21:12:09
【问题描述】:

在我的 JSF 页面中,我很少看到 Links{link1,link2,link3,link4}-{Student Id's}.

我尝试的是,当我点击链接时,它会在 "NEW WINDOW". 中打开学生信息

当我点击下一个链接时,它正在打开一个新窗口,但我想在同一个新窗口中打开,即目标应该是打开的同一个新窗口。

图解表示:

我尝试从 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>

编辑后@Nosnhoj 回复:When i Click on any of the SID Link then there Details Should Opened in the "Same NEW Window".

【问题讨论】:

  • @Makky 我已经粘贴了一个示例代码,希望它可以帮助。因为,我是 JSF 的新手,Primefaces。我无法弥补。如果我在任何地方出错,请纠正我。感谢回复
  • 您尝试过之前问题中的答案吗?
  • 你不能使用对话框而不是在不同的选项卡中显示结果吗??
  • @Makky 我的要求是我有一个要显示的学生信息页面,所以我想将它们显示为一个单独的页面。对话框是一个很好的对话框,它可能很有用,但它不是我想要的......我为下面的答案添加了评论

标签: jsf jsf-2 primefaces


【解决方案1】:

使用&lt;h:commandLink&gt; 而不是&lt;p:commandLink&gt;

我尝试了您的代码并进行了如下一些更改:

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

&lt;p:commandLink&gt;更改为&lt;h:commandLink&gt;,并在backing bean中调用一个方法来设置选定的Student。(对于New Window应该需要学生信息。)

这里是studentController.selectStudent的代码:

private Student selectedStu;

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

以下是New Window的代码:

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

这只是显示所选学生的姓名,以及您想要的另一个链接。
请注意,target 属性是 _self,因为您希望新页面显示在同一窗口中。

最后,anotherinfo.xhtml 只是我通过 NetBeans 创建的一个空白页面,因此无需发布它。

您可能会看到以下内容:

点击“Johnson”的id后:

点击“另一个链接”后:


更新

正如@User2561626 解释他/她的意图,我将我的答案更新如下:
如果您想要一个 New Window 弹出而不是 New Tab,您应该将代码更改为:

<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>

我把&lt;h:commandLink&gt;改回&lt;p:commandLink&gt;,把action改成actionListener,因为我们要先设置选中的Student,最后我在oncomplete属性中添加window.open,这样新页面就会打开在新窗口中,但不在标签中。
当然我们需要编辑方法studentController.selectStudent(),像这样:

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

如您所见,返回类型现在是 void,此方法唯一要做的就是设置选定的 Student。
希望这可能会有所帮助。

【讨论】:

  • 答案很好!它工作正常。但是,我的要求是,如果我们点击 sid 考虑在您的场景中,您点击了 sid:2,然后“新窗口”打开了。然后如果我再次点击 sid:4,那么相同的“新窗口”应该刷新并显示 StudentInfo。
  • 然后把target="_blank"改成target="_new"
  • 它如何在打开的同一个窗口中打开,然后对于我单击的每个链接,它都会打开一个新窗口,旧窗口仍然打开。
  • target="_new" 工作正常。但是,我很惊讶它怎么能在同一个窗口中打开。它作为新的“选项卡”打开一个窗口,我们可以让它在一个“新窗口”
  • 效果很好!但与第二次单击一样,它并没有关注新窗口。我尝试使用window.open('studentinfo.xhtml', 'newwindow', 'width=300, height=250').focus(); 成功了!感谢您的支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
相关资源
最近更新 更多