点击JSF中的<p:commandButton>如何打开一个新窗口

3

我想通过在JSF中点击<p:commandButton>按钮来打开一个新的弹出窗口。

下面是我的代码:

<h:inputText style="width:42%"value="#{xxbean.values}" rendered="#{xxBean.yy == true}" 
     onblur="cc;" maxlength="6">
</h:inputText> 
<p:commandButton value="FindPhone" id="xxne" actionListener="#{xx.findPhoneSearch}"
    oncomplete="window.open('#{xx.wpUrl}', '_blank')" 
    rendered="#{xx.editCmdActionflg == true }"  async="false">
    <f:param name="pmid" value="#{xx.Details.uid}"/>                                   
</p:commandButton>

我正在调用findPhoneSearch方法,就像上面在命令按钮的actionlistener中给出的那样。

这是findPhoneSearch方法:

public void FindphoneSearch(ActionEvent event) {



        String param = "";

        Map<String, String> params = FacesContext.getCurrentInstance()
                .getExternalContext().getRequestParameterMap();

        String expression = "^[a-z0-9]+$";
        Pattern pattern = Pattern.compile(expression);

        if (params.get("pmid") != null) {
            String t_pmid = params.get("pmid");
            Matcher matcher = pattern.matcher(t_pmid);
            if (matcher.matches()) {
                param = "/cgi-bin/Findphones.pl?id=" + t_pmid.trim();
            }
        }

        if (params.get("lpid") != null) {
            String t_lpid = params.get("lpid");
            Matcher matcher = pattern.matcher(t_lpid);
            if (matcher.matches()) {
                param = "/cgi-bin/Findphones.pl?id=" + t_lpid.trim();
            }
        }

        String findphoneUrl= "http://Findphone.com" + param;
        wpUrl = findphoneUrl;


    }

我的问题是窗口打开时是空白的,没有传递我正在框架中分配给wpurl的URL。

请帮助我解决这个问题。


尝试在您的commandButton中添加属性update="@all"。我认为您的问题是,您正在尝试使用ajax调用而没有更新视图以获取答案,因此wpUrl的值是旧的(为空)。 - Yamada
@yamada:我也尝试过,但没有帮助。我正在尝试使用以下代码将参数传递给后端bean:<f:param name="pmid" value="#{xx.Details.uid}"/>当我点击命令按钮时,但我无法获取该值。 - Karthik
你可以使用 onclick="target='_blank'" https://stackoverflow.com/a/54010074/5626568 - ℛɑƒæĿᴿᴹᴿ
1个回答

4
在PrimeFaces组件的oncomplete属性中,EL表达式#{...}在首次显示该按钮所在页面时进行求值,而不是在按下该按钮后进行。因此基本上,您正在处理页面呈现时的值,而不是在操作方法中更改的值。
最好更新内联脚本而不是执行oncomplete
<p:commandButton ... update="openWindow" />
<h:panelGroup id="openWindow">
    <h:outputScript rendered="#{not empty xx.wpUrl}">
        window.open('#{xx.wpUrl}', '_blank')
    </h:outputScript>
</h:panelGroup>

不要忘记从按钮中删除async="false"

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