edelweiss
edelweiss

Reputation: 13

JSF Primefaces and ajax listener, button not shown when rendered

I am using JSF and Primefaces to develop a JavaEE application. Each time I press a button(Authenticate), a BUTTON (Button) and a message(Message) must be shown. The problem is that the message is shown, but not the button. What could I do to show the button?

Here is my code:

ManagedBean IdCertificateManagedBean.java

String  m_strRoute;
String  m_strMessageRegister;
boolean m_bRegistrationCorrect;


//Getters and Setters 

//Constructor
public IdCertificateManagedBean()
{
        m_strMessageRegister = "";
        m_strRoute = "";
        m_bRegistrationCorrect = false;
}


 public void authenticateUser()
 {      
     try
    {
       int a=5;

        if (a ==5)
        {   

                m_strMessageRegister = "hello";
                m_bRegistrationCorrect = true; 
                m_strRoute = "/admin/menuadmin"; 
         }    
          else 
          { 
                m_strMessageRegister = "ERROR";
          } 
     }  
     catch ( Exception e)
    {
            m_strMessageRegister = "Error";
    }

  }

}

Facelet

<p:commandButton value="Authenticate">
    <p:ajax listener="#{IdCertificateManagedBean.authenticateUser()}" update="Message, Button" />
</p:commandButton>

<h:outputText id="Message" value="#{IdCertificateManagedBean.MessageRegister}" />                   
<p:commandButton id="Button" value="Go" action="#{IdCertificateManagedBean.route}" rendered=" {IdCertificateManagedBean.registrationCorrect}" />

Upvotes: 0

Views: 193

Answers (1)

amedeo avogadro
amedeo avogadro

Reputation: 595

You must wrap your <h:commandButton /> inside a container as <h:panelGroup /> and then refresh the container. The problem is that on first page load your button is not rendered so it is not inserted inside the DOM of the page. On page refresh you're trying to refresh it but you can't do it because the button isn't on the page. A solution could be this

<h:panelGroup layout="block" id="buttonDiv">
    <h:commandButton ... />
</h:panelGroup>

and then in your ajax you have to refresh buttonDiv

Upvotes: 1

Related Questions