0

the problem is to cacth new values after i have previous values from the DB and overwrite them, i wanna save the new values in my beans "variables" and also show the values from de DB in this component, but if i put "Usuario.nombre", it doesnt show the values from the DB, and if i put "datos.nombres" it shows the values from the DB, please help

Here is my xhtml file

        <p:dataTable style="width:450px" value="#{DAOUsuario.Listar()}" var="datos">
            <p:column headerText="Datos Personales" style="background:#19708F ; color: white">
                <p:outputLabel value="Nombre"/>

                <h:inputText value="#{datos.nombre}"/>                                        
                <br/>
                <p:outputLabel value="Clave" />

                <h:inputText value="#{datos.clave}"/>                                        
                <br/>
                <h:commandButton value="Modificar" action="#{DAOUsuario.Modificar()}">                    
                </h:commandButton>                        
            </p:column> 

        </p:dataTable>

    </h:form>

</body>

Here is my Class which have methods to connect to the DB

public void Modificar() throws Exception {

    FacesContext context = FacesContext.getCurrentInstance();
    Usuario us = (Usuario) context.getApplication().evaluateExpressionGet(context, "#{Usuario}", Usuario.class);

    String sql = "UPDATE usuario SET nombre = '" + us.getNombre() + "', clave = '" + us.getClave() + "' "
            + "WHERE codigousuario = 2";

    try {
        this.Insert(sql);
    } catch (Exception ex) {
        throw ex;
    }
}

and here is my Bean Usuario:

public class Usuario {

    private int codigo;
    private String nombre;
    private String clave;

    public int getCodigo() {
        return codigo;
    }

    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getClave() {
        return clave;
    }

    public void setClave(String clave) {
        this.clave = clave;
    }

}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
MitoCode
  • 319
  • 2
  • 10
  • 25

1 Answers1

0

Just send the Usuario object as a parameter to your Modificar function:

public void Modificar(Usuario us) throws Exception {
    //just to maintain compatibility with your current Usuario us local variable
    //FacesContext context = FacesContext.getCurrentInstance();
    //Usuario us = (Usuario) context.getApplication().evaluateExpressionGet(context, "#{Usuario}", Usuario.class);

    //code goes here...
}

And modify the JSF code to send it in your <p:commandButton>:

<p:dataTable style="width:450px" value="#{DAOUsuario.Listar()}" var="datos">
    <p:column headerText="Datos Personales" style="background:#19708F ; color: white">
        <p:outputLabel value="Nombre"/>
        <h:inputText value="#{datos.nombre}"/>
        <br/>
        <p:outputLabel value="Clave" />
        <h:inputText value="#{datos.clave}"/>
        <br/>
        <h:commandButton value="Modificar" action="#{DAOUsuario.Modificar(datos)}" />
    </p:column> 
</p:dataTable>

Outside of the question scope, as a recommendation, don't use your DAO classes as managed beans, it would be better to leverage the coupling in your application by having at least these logical layers:

  • Controller layer (for the JSF/CDI managed beans)
  • Business/Service layer (business logic goes here)
  • Data access layer (here are the DAO classes)

Another recommendation: please follow the JavaBeans naming conventions for class and function naming, you can have a quick review here. As an example, your DAOUsuario functions should be listar instead of Listar and modificar instead of Modificar.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Veo que eres de Peru, eso que me dices lo intente, pero como parametro de mi metodo Modificar no puedo recibir Usuario us y lineas mas abajo volver a instanciar otro Usuario con la mismo variable, me sale un error de definicion que ya existe variable "us" ... y otra pregunta podrias explicarme nuevamente de las capas logicas y porque no deberia usar el dato para manejar mis beans...gracias – MitoCode Feb 25 '13 at 17:21
  • @J.A. main language in SO is English, remember that other people in the world can read the Q/A and comments. Back to your problem, you can send a `Usuario us` parameter and not use the `Usuario us` local variable at all, check the `Modificar` function that I've posted (I commented the `Usuario us` and post that the parameter calls `us` in order to maintain compatibility with current code that already uses `us`), of course you can change the name of the parameter, but that's Java basic programming. – Luiggi Mendoza Feb 25 '13 at 17:30
  • @J.A. about using logic layers, please refer [here](http://stackoverflow.com/a/13012973/1065197) to get a better understanding. – Luiggi Mendoza Feb 25 '13 at 17:37
  • yes i got it, i do that, i pass the "Usuario" parameter to my mehod "modificar" and i comment the lines of FacesContext and Usuario, the method works, it recieve the parameter but this is the real problem.... – MitoCode Feb 25 '13 at 17:41
  • First: i have Name: admin --- (inpuText with default values from DB) Password: admin --- (inpuText with default values from DB) Ok button ---(only to test my method "modificar", here I call that method with my new values that i enter, and only capture the old values and not the new ones i enter) – MitoCode Feb 25 '13 at 17:43
  • my variable "datos.nombre" only capture the value from DB, but if i insert a new value, only capture me null – MitoCode Feb 25 '13 at 17:49