Opening a new window by clicking <p:commandButton> in JSF

11,837

The EL #{...} in oncomplete attribute of a PrimeFaces component is evaluated when the page with the button is displayed for the first time, not after the button is pressed. So basically, you're dealing with the value as it was while the page is rendered, not with the value which is changed in action method.

You'd better ajax-update an inline script instead of performing 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>

Don't forget to remove async="false" from the button.

Share:
11,837
Karthikeyan
Author by

Karthikeyan

Always a guy strive to learn new things and yes i agree, im not trying my best :) yet am there slowly moving to the end of life ..peace !!

Updated on July 14, 2022

Comments

  • Karthikeyan
    Karthikeyan almost 2 years

    I am trying to open a new pop up window by clicking a button <p:commandButton> in JSF.

    Here is my code,

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

    I am calling he method findPhoneSearch like the one given above in actionlistener inside the command button ,

    Here is the findPhoneSearch method ,

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

    My problem is the window is open blank without passing the url that I am framing which is assigned in wpurl.

    Please help me to resolve this issue.