Get the next number using JSF + PrimeFaces

1

I have the following problem, which I do not know how to solve. I want that when making the registration in a table I already propose in the view a following number. I managed to arm the query, but for some reason it does not refresh in the view. Maybe someone can tell me what is the problem I'm having:

 <p:outputLabel for="txtCuenta" value="Cuenta" />
                        <p:inputText id="txtCuenta" value="#{xxxController.cuenta.numero}" maxlength="05" size="05">
                            <p:ajax event="focus" update="txtCuenta" listener="#{xxxController.getUltimaCuenta}" />
                        </p:inputText>      
                        <p:message for="txtCuenta"></p:message>

And in the xxxController I have the method getUltimaAccount, which works:

  public short getUltimaCuenta(){
    short numero=0; 
    numero=cuentaEJB.ultimo();
    numero++;
    return numero;
}

How do I get the value returned by the getUltimaCuenta method to appear in the inputText txtCuenta and store clear?

    
asked by Fernando 21.09.2016 в 19:31
source

2 answers

1

Thank you very much for your reply MitsuG!,

However I managed to "update" view and show me the next number, because the method was executed, only that it would not appear. Adding the following lines worked as expected:

<p:focus context="pg"/> 

And so the view is:

 <h:form>
                <p:focus context="pg"/> 
                <p:messages id="mensaje" autoUpdate="false" severity="info,fatal" showSummary="false" showDetail="True"></p:messages>

                <fieldset>
                    <legend>Alta de Cuentas</legend>
                    <h:panelGrid columns="3" id="pg">
                        <p:outputLabel for="txtCuenta" value="Cuenta" />
                        <p:inputText id="txtCuenta" value="#{xxxController.cuenta.numero}" maxlength="05" size="05">
                            <f:ajax event="focus" listener="#{xxxController.getUltimaCuenta}" render="txtCuenta"/>                    
                        </p:inputText>      
                        <p:message for="txtCuenta"></p:message>

Thank you for your time, and good disposition!

Greetings Fernando

    
answered by 06.10.2016 в 00:21
0

The logic is simple. Let's see by steps:

  • Listen by event focus
  • Associate said method with a backing bean.
  • JSF executes the associated method in the bean backing and saves the result of the operation in a variable.
  • Once the value is saved, using the attribute render or update if you are using PrimeFaces, the value obtained from the execution of the method on the label is rendered.
  • Now let's represent this logic with code:

    Vista

    <h:outputLabel value="#{controller.ultimaCuenta}" />
    <h:inputText>
      <f:ajax event="focus" render="txtCuenta"
              listener="#{controller.consultarUltimaCuenta()}" />
    </h:inputText>
    <p jsf:id="txtCuenta"></p> <!-- JSF 2.2 passtrough -->
    

    Backing bean

    private short ultimaCuenta;
    
    ...
    
    public void consultarUltimaCuenta() {
        ultimaCuenta = EJBCuenta.ultimo();
    }
    
    public short getUltimaCuenta() {
        return ultimaCuenta;
    }
    

    It is important to clarify what @LuiggiMendoza did to you:

      

    considers that in JSF the getter methods can be called more than one   time, so it is not recommended that there be business logic or in   get neither in set of your managed beans.

    You can see the explanation here .

        
    answered by 22.09.2016 в 00:26