当我点击任何链接时,它应该在JSF,Primefaces的同一个新窗口中打开

 爱lovely壮壮_366 发布于 2023-02-07 11:36

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

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

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

图形表示:

目标链接到同一窗口

我在新窗口中收集STACKOVERFLOW 打开页面时试过的一段代码:


        
            
                    
                     
                         
            
        
            
                  
                    
                
            
            
               
            
            
                
            
    

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

1 个回答
  • 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', ');">  
        <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.

    2023-02-07 11:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有