0

My intention is to display a message to the user in a rich: popupPanel after insert a record in BD.

With the code below I get to do what I want, but with a problem. Only works when I type all page fields correctly. If one of the fields does not pass in validation, the validation message is displayed in the h:messages, and the page does not change, even if I change the contents of the field and click the button again.

What could be wrong?

  • page

    <h:outputLabel value="Nome:" for="nome1" />
    <h:inputText id="nome1" maxlength="50" required="true" requiredMessage="Informe um nome" immediate="true"/>
    
    <h:outputLabel value="Logradouro:" for="logradouro" />
    <h:inputText id="logradouro" maxlength="50" required="true" requiredMessage="Informe o logradouro" immediate="true"/>
    
    <h:outputLabel value="Bairro:" for="bairro" />
    <h:inputText id="bairro" maxlength="45" required="true" requiredMessage="Informe o bairro" immediate="true" />
    
    <h:outputLabel value="CEP:" for="cep" />
    <h:inputText id="cep" maxlength="8" required="true" requiredMessage="Informe o CEP" immediate="true">
        <f:convertNumber integerOnly="true" groupingUsed="false" maxIntegerDigits="8" />
    </h:inputText>
    
    <a4j:commandButton id="btnInserirDizimista" value="Inserir" oncomplete="#{rich:component('pnlOk')}.show()" render="pnlMessages outPnlOk" actionListener="#{dizimistaMB.inserirDizimista}"/>
    
    <a4j:outputPanel id="outPnlOk" ajaxRendered="true">
    <rich:popupPanel id="pnlOk" modal="true" height="150">
            <h:graphicImage value="/images/info.jpg" />
            <h:outputText id="textoModalOk" value="#{dizimistaMB.textoModal}" />
            <h:commandButton value="OK" action="dizimista?faces-redirect=true"
                onclick="#{rich:component('pnlOk')}.hide(); return false;" >
                <a4j:ajax execute="formPnlOk" />
            </h:commandButton>
        </rich:popupPanel>
    </a4j:outputPanel>
    

  • managed bean

    public void inserirDizimista(){
        try{
            defineDizimista("formNovoDizimista");
            dizimistaFacade.criar(dizimista);
            setTextoModal("Dizimista inserido com sucesso");
            System.out.println("Dizimista '" + dizimista.getNome1() + "' inserido com sucesso");
            dizimista = null;
        } catch (DAOException e) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), null);
            JSFHelper.getFacesContext().addMessage("ATENÇÃO!", msg);
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void defineDizimista(String nomeForm){
        Map<String, String> dados = getDadosFormulario(nomeForm);
        dizimista.setNome1(dados.get("nome1"));
        dizimista.setLogradouro(dados.get("logradouro"));
        dizimista.setBairro(dados.get("bairro"));
        dizimista.setCep(Integer.parseInt(dados.get("cep")));
    }
    
    private Map<String, String> getDadosFormulario(String form){
        Map<String, String> dados = new HashMap<String, String>();
        dados.put("nome1", JSFHelper.getRequestParameter(form +
            UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "nome1"));
        dados.put("logradouro", JSFHelper.getRequestParameter(form +
            UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "logradouro"));
        dados.put("bairro", JSFHelper.getRequestParameter(form +
            UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "bairro"));
        dados.put("cep", JSFHelper.getRequestParameter(form +
            UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "cep"));
        dados.put("cidade", JSFHelper.getRequestParameter(form +
            UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "cidade"));
        return dados;
    }
    
roggaioso
  • 15
  • 2
  • 6
  • You shouldn't use an [actionListener to do an action method's job](http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener). You also shouldn't silent your exceptions. Does anything print on the console when validation fails? – Elias Dorneles Aug 03 '12 at 01:57
  • Thanks for your reply and this link. I honestly still didn't know the difference between action and actionlistener. As regards the exception, I'm sorry, but just wanted to simplify the code. Thank you for your comment. – roggaioso Aug 04 '12 at 02:14
  • you're welcome. did you solved your problem, then? – Elias Dorneles Aug 04 '12 at 02:15
  • Even with your suggestion regarding the action did not work, if you can help me, thank you. I changed to action but clicking the validation messages are displayed in h:messages, but the popupPanel appears without any text. I wanted the same were only displayed in case of success. And as is, even entering the fields correctly the insert method is not called. – roggaioso Aug 04 '12 at 02:21
  • can you post the full code of your MB code as it is now? – Elias Dorneles Aug 04 '12 at 02:28
  • Yeah, I just saw it, but I've already posted an answer, check it out. Anyway, why don't you create fields in your MB to bind the values for `nome`, `logradouro`, etc, like you would for a normal JSF app? [see the Hello World here](http://stackoverflow.com/tags/jsf/info) – Elias Dorneles Aug 04 '12 at 03:20
  • Regarding your last comment, my MB uses a normal bean (Dizimista.class injected by Spring), and this has the attributes listed. I'll check your response and return later. Thank you once again. – roggaioso Aug 04 '12 at 03:27
  • Then you could do ``, no? – Elias Dorneles Aug 04 '12 at 03:31
  • Perfect! It Worked! I was using javascript (oncomplete) after receiving as suggested in another forum, but was not able to move forward from this point. With your help I will be able to continue. Very very thanks! – roggaioso Aug 04 '12 at 03:43
  • You're welcome. bom trampo aí! :) – Elias Dorneles Aug 04 '12 at 03:46

1 Answers1

1

When you render the contents of a rich:popupPanel through AJAX (rendering from a4j:commandButton), it may be best choose to display it conditionally using its show attribute, instead of calling Javascript code (oncomplete).

So, I suggest you just add a condition for showing the popupPanel. Assuming that textoModal is null upon initialization, and your bean is a request scoped, you can just use something like this:

<rich:popupPanel id="pnlOk" modal="true"
       show="#{dizimistaMB.textoModal != null}" ...>

You may want to create a method in the backing bean something like public boolean isShowTextoModal() that may implement better rules than simply checking if a field is not null.

Then, you can change your button to this:

<h:commandButton id="btnInserirDizimista" value="Inserir"
       render="pnlMessages outPnlOk"
       action="#{dizimistaMB.inserirDizimista}" />

... and remove the ActionEvent argument from your method inserirDizimista, making it an action method instead of a actionListener.

This should get things going for you.

Community
  • 1
  • 1
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107