p:commandLink无法在新窗口/选项卡中打开页面

22

我试图创建一个链接,在新的窗口/标签页中打开一个页面并显示一些来自后端bean的消息,但无法实现,想知道原因是什么?

这是我的xhtml文件:

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

这是我的Msg.xhtml页面

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

这是我的testing.java文件

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

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


上面的代码无法打开一个新的选项卡/窗口,我尝试像下面这样做,成功在新的选项卡中打开了新页面,但是消息为空,当我调试时,它成功地调用了监听器getMessage,我想知道为什么在msg.xhtml页面中消息为空? 提前致谢....

<p:commandLink actionListener="#{testing.getMessage}" oncomplete="window.open('msg.xhtml')">broadcast Msg</p:commandLink>

1
尝试在您的commandLink的第一个版本中设置ajax="false" - partlov
你的 Bean 范围是什么? - neni
@heng heng 希望这对你有所帮助 - 09Q71AO534
8个回答

19

<p:commandLink>存在一些Ajax问题,请使用 <h:commandLink> 替代。

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

<p:commandLink> 改为 <h:commandLink> ,你的代码正常工作。


4
“一些Ajax问题”。这怎么能算是一个答案?你的解决方案是正确的,但你完全解释错了。 - BalusC
1
是的,我同意。我只是提供了一个解决方法。我应该把它发布为评论。 - neni
2
然而,这绝对是最好的答案。我会选择“一些ajax问题”。 - Andrew
1
使用<p:commandLink>,您可以通过设置属性ajax=false来使其正常工作。 - Yohanes Khosiawan 许先汉
@YohanesKhosiawan许先汉:哪个PF版本?因为你的陈述与其他答案相矛盾。 - Kukeltje
@YohanesKhosiawan许先汉:请检查一下。如果是这样,我们需要明确地将其添加到此答案和其他5个赞同的答案中。 - Kukeltje

6

两者都无法工作,因此问题不在primefaces ajax中。问题在于p:commandLink的target属性不起作用...

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

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

所以我们必须使用

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

5
我是这样做的:

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

使用一些 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>

在这个例子中,您可以打开一个新窗口,希望这对您有所帮助!

1
如果您正在使用远程命令,则可以按照以下方式操作。
<p:remoteCommand name="lookAndBookNewTab"
     process="@this" action="#{lookAndBookMB.onPageLoadForNewTab()}"
     rendered="#currentUserManager.
     hasPermission('LookAndBook','View')}"             update=":messageForm:growl" />

而我的JavaScript代码:

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

服务器端的操作方法:
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 ""; }

0

在你的控制器代码中尝试这个,它对我来说完美地工作了。

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();   

0

如果有人想知道,使用Primefaces 9、10、11可以这样做

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

如果您不想使用h:commandLink,可以使用此方法


-1

像这样,

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

祝你好运!


请在您的答案中添加一些文本和解释。 - Kukeltje

-2

像这样

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

请参见:在此输入链接描述


3
为什么这个明显不工作的答案(ajax="false"完全缺失),还带有一个完全无关的<span>和可能会与jquery-ui库(PrimeFaces自带)产生冲突,却被点赞了?/review里面还有瞎子吗? - BalusC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接