Search code examples
jsfjsf-2primefaces

Primefaces p:dialog keeps reopening


My p:dialog keeps loading on and on, I need it to appear only once. Does anyone know how to do it?

    <h:body style="background: url('img/background/teste/brushed_alu.png')!important" onload="dialogAtivacao.show();">
    <script type="text/javascript">
    $(document).ready(function() {
        document.getElementById("j_username").focus();
    });
    </script>

    <p:dialog id="dialogAtivar" header="Ativação de empresa"  showEffect="drop" hideEffect="drop" resizable="false"
              widgetVar="dialogAtivacao" modal="true" closable="true" rendered="#{sessionScope['SPRING_SECURITY_LAST_EXCEPTION'].message == 'ATIVACAO'}">
        <ui:include src="pages/ativacao/AtivacaoEmpresa.xhtml"/>
    </p:dialog>
    ...

The button:

  <p:panel  styleClass="panelBotaoLogin" >
        <h:commandButton id="saveButton" action="#{login.doLogin()}" value="  Entrar" styleClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only botaoEntrar"/>
  </p:panel>

The login() in LoginBean :

    public String doLogin() throws IOException, ServletException {
           ExternalContext context =  FacesContext.getCurrentInstance().getExternalContext();
           RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_check?j_username=" + username + "&j_password=" + password);
           dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());
           FacesContext.getCurrentInstance().responseComplete();

           return null;
     }

I have one customAuthenticationProvider that returns 'ATIVACAO' when the database is empty, so I need open this dialog to insert data, but it keeps reopening(closes and immediately reopens).


Solution

    1. <h:body onload="dialogAtivacao.show();"/> translates into when the <body> tag of the HTML is loaded, display the popup. The <body> tag will be reloaded when a full page reload of the view takes place

    2. <h:commandButton id="saveButton" action="#{login.doLogin()}" value=" Entrar" styleClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only botaoEntrar"/> is going to trigger a full page refresh everytime the button is clicked.

    Together, it becomes, everytime I click this button, reload the entire page and show the dialog as a result

    Use an ajax command component instead:

     <p:commandButton id="saveButton" action="#{login.doLogin}" value="  Entrar" styleClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only botaoEntrar"/>
    

    This way, an ajax request is triggered and the onload is triggered only once