p:commandLink fails to open page in new window/tab
Asked Answered
C

8

22

I'm trying to create a link to open a new page in a different window/tab and display some msg from backing bean but fail to do it, wonder know why?

here is my xhtml file:

<html:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:ui="http://java.sun.com/jsf/facelets">
  <h:body>
    <h:form id="form66">
    <p:commandLink actionListener="#{testing.getMessage}" action="msg.xhtml" target="_blank">get Msg</p:commandLink>
    </h:form>
  </h:body>
</html>

here is my Msg.xhtml page

<HTML xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:ui="http://java.sun.com/jsf/facelets">
  <h:head>
    <title>testing</title>
  </h:head>
  <h:body>
    <div class="div">
      <p:panel>
        <f:facet name="header">
          testing
        </f:facet>
        <div class="paddingForPanel">
          <h:outputText value="#{testing.msg}" escape="false"/>             
        </div>
      </p:panel>            
    </div>
  </h:body>
</HTML>

here is my testing.java

public void getMessage() {      
    this.msg = "haha";
}

private String msg;
public String getMsg() {
    return msg;
}
public void setMsg(String msg) {
    this.msg = msg;
}


the code above fail to open a new tab/window, I try to do like below, it success to open the new page in new tab but the msg is empty, when I debug, it got success call the listenner getMessage, I wonder know why the msg is empty in the msg.xhtml page? Thanks in advance....

<p:commandLink actionListener="#{testing.getMessage}" oncomplete="window.open('msg.xhtml')">broadcast Msg</p:commandLink>
Certain answered 6/2, 2013 at 8:23 Comment(3)
Try to set ajax="false" in the first version of your commandLink.Jaal
What is your Bean scope ?Mump
@heng heng Hope this is helpfullBaeda
M
19

<p:commandLink> has some ajax issues, Use <h:commandLink> instead.

 <h:commandLink actionListener="#{testing.printMessage}" action="/Msg.html" target="_blank">get Msg</h:commandLink>

changed <p:commandLink> to <h:commandLink> and your code is working fine.

Mump answered 6/2, 2013 at 10:22 Comment(6)
"some ajax issues" How's that an answer? Your solution is right, but you're explaining it completely wrong.Subatomic
yes I agree. I just gave a workaround. I should have posted it as comment.Mump
And yet, this is by far the best answer. I'll take "some ajax issues".Salubrious
with <p:commandLink>, you can make it work by having attribute ajax=falseMaite
@YohanesKhosiawan许先汉: Which PF version? Since your statement contradicts the other answerBrooklime
@YohanesKhosiawan许先汉: Please check. It if so, we nee to add this explicitly to this answer and to the other 5 upvoted one.Brooklime
E
6

Both are not working so issue is not in primefaces ajax. Issue in target attribute of p:commandLink its not working...

<p:commandLink value="New Window" ajax="false" target="_blank" action="test"/>

<p:commandLink value="New Window" target="_blank" action="test"/>

SO we have to use

<h:commandLink value="New Window" target="_blank" action="test"/>
Endoskeleton answered 14/4, 2014 at 5:49 Comment(0)
J
5

I do it like this:

<p:commandLink id="link" actionListener="#{testing.getMessage}" 
    oncomplete="popupwindow('msg.xhtml', 'newwindow');" >  
    <h:outputText value="broadcast Msg" />
</p:commandLink>

With some javascript:

<script type="text/javascript">    
function popupwindow(url, title) {      
    window.open(url , title, "toolbar=no, scrollbars=yes, resizable=yes, top=170, left=170, width=800, height=600");        
}    
</script>

In this example you open a new window, hope its helps!

Jamisonjammal answered 20/9, 2016 at 6:46 Comment(0)
S
1

If you are using remotecommand then you can do like this

<p:remoteCommand name="lookAndBookNewTab"
     process="@this" action="#{lookAndBookMB.onPageLoadForNewTab()}"
     rendered="#currentUserManager.
     hasPermission('LookAndBook','View')}"             update=":messageForm:growl" />

And my javascript:

<script> 
    if(event.ctrlKey &amp;&amp; event.shiftKey &amp;&amp; event.keyCode == 119) {
     lookAndBookNewTab(); 
    event.preventDefault();
</script>

ServerSide action method:

public String onPageLoadForNewTab(){
    HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); String url = req.getRequestURL().toString();
        url=(url.substring(0, url.length() - req.getRequestURI().length()) + req.getContextPath() + "/"); url=url+navigationpagename+".jsf"; RequestContext.getCurrentInstance().
        execute("window.open('"+url+"','_blank')");** 
    return ""; }
Seiden answered 18/1, 2016 at 12:29 Comment(0)
C
0

Try this in your controller code, it worked for me perfectly.

HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
            HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            String uri = req.getRequestURI();
            res.getWriter().println("<script>window.open('" + myUrl + "','_blank', 'location=yes,height=600,width=800,scrollbars=yes,status=yes'); window.parent.location.href= '"+uri+"';</script>");
            FacesContext.getCurrentInstance().responseComplete();   
Carlacarlee answered 24/8, 2017 at 7:30 Comment(0)
B
0

In case anyone is wondering about it, using Primefaces 9,10,11 doing this

<p:commandLink value="New Window" ajax="false" target="_blank" action="test"/>

does work in case you do not want to use h:commandLink

Bolduc answered 22/12, 2021 at 14:51 Comment(0)
D
-1

like this,

<h:link target="_blank" value="Other page" outcome="your url"></h:link>

Good luck!

Dorran answered 19/1, 2016 at 17:2 Comment(1)
Please add some text and explanation to your answer.Brooklime
V
-2

like this

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
     'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>

     <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">


    <h:head>
        <h:outputStylesheet library="base" name='jquery-ui-1.8.16.custom.css' />
    </h:head>
    <span style="background-color: rgb(192, 192, 192);">
      <h:form target="_blank">
        <p:commandButton value="Run" action="#{bean.createReport}"  />
      </h:form>
    </span>
    </html>

see:enter link description here

Vitreous answered 5/8, 2013 at 4:28 Comment(1)
Why in earth is this non-working answer (ajax="false" is completely missing), flavored with a completely irrelevant <span> and potentially conflicting jquery-ui library (PrimeFaces ships with its own), upvoted? Are there still blind monkeys in /review?Subatomic

© 2022 - 2024 — McMap. All rights reserved.