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

时间:2013-02-06 08:23:46

标签: jsf jsf-2 primefaces commandlink

我正在尝试创建一个链接,在另一个窗口/选项卡中打开一个新页面并显示一些来自支持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;
}


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

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

7 个答案:

答案 0 :(得分:18)

<p:commandLink>有一些ajax问题,请改用<h:commandLink>

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

<p:commandLink>更改为<h:commandLink>,您的代码运行正常。

答案 1 :(得分:3)

两者都不起作用,所以问题不在于主要的ajax。 p:commandLink的目标属性问题,它无法正常工作......

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

答案 2 :(得分:2)

我这样做:

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

在这个例子中,你打开一个新窗口,希望它有所帮助!

答案 3 :(得分:1)

如果您使用的是remotecommand,那么您可以这样做

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

ServerSide操作方法:

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 ""; }

答案 4 :(得分: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();   

答案 5 :(得分:-1)

像这样,

<h:link target="_blank" value="Other page" outcome="your url"></h:link>
祝你好运!

答案 6 :(得分:-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>

见:enter link description here