1

i have a datatable with one row , i need to edit the fields of this row so i have a few inputText with the values, but when i edit them and click on the commandbutton(that calls the method "actualizarUsuario" the values are passed as null.

this is my bean code:

@ManagedBean(name = "user")
@ViewScoped
public class userDetalles implements Serializable {



    private Usuario u;
    private usuarioController controlador;
    Rol rol;

    private long selection;
    private long selectionrol;
    Agrupacion agrupacion;
    private Privilegio privilegio;
    private RolController controladorRol;
    private ControladorAgrupaciones controladorAgrup;
    private String nombres;
    private String apellidoP;
    private String apellidoM;
    private Boolean check;

    @PostConstruct
    public void init() {

       rol= new Rol() ;
       u=new Usuario();
       agrupacion=new Agrupacion();
       privilegio=new Privilegio();
       controlador= new usuarioController();
       controladorRol=new RolController();
       controladorAgrup=new ControladorAgrupaciones();
       Usuario u=new Usuario();
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();

        //Obtener parametros del request
       Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
       long iduser = Long.valueOf(parameterMap.get("id_usuario"));

       this.u=controlador.getUser(iduser);
    }

    public Usuario getU() {
        return u;
    }

    public void setU(Usuario u) {
        this.u = u;
    }

    public long getSelection() {
        System.out.println("selection value----------->"+selection);
        return selection;
    }

    public void setSelection(long selection) {
        this.selection = selection;
    }

    public long getSelectionrol() {
        return selectionrol;
    }

    public void setSelectionrol(long selectionrol) {
        this.selectionrol = selectionrol;
    }

    public String getNombres() {
        return nombres;
    }

    public void setNombres(String nombres) {
        this.nombres = nombres;
    }

    public String getApellidoP() {
        return apellidoP;
    }

    public void setApellidoP(String apellidoP) {
        this.apellidoP = apellidoP;
    }

    public String getApellidoM() {
        return apellidoM;
    }

    public void setApellidoM(String apellidoM) {
        this.apellidoM = apellidoM;
    }

    public Boolean getCheck() {
        return check;
    }

    public void setCheck(Boolean check) {
        this.check = check;
    }


    public void actualizarUsuario(){

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, String> parameterMap = (Map<String, String>)   externalContext.getRequestParameterMap();
        nombres=parameterMap.get("nombres");
        apellidoP=parameterMap.get("apellidoP");
        apellidoM=parameterMap.get("apellidoM");
        check=Boolean.parseBoolean(parameterMap.get("check"));
        //test
        System.out.println(nombres+" "+apellidoP+" "+apellidoM+" "+check);
        u.setNombres(nombres);
        u.setApellidoPaterno(apellidoP);
        u.setApellidoMaterno(apellidoM);
        u.setActive(check);
        controlador.saveUsuario(u);
    }



}

and this is my view:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <div class="container">
        <h:panelGroup id="Users">
            <h:form id="Form">
                <h2>Detalles Usuario</h2>

                <h:dataTable id="users" value="#{user.u}"  styleClass="table table-striped table-bordered" headerClass="sorting_asc"
                             rowClasses="odd,even">
                    <h:column>
                        <f:facet name="header">#</f:facet>
                        #{user.u.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Identificador</f:facet>
                        <h:inputText id="identificador" value="#{user.u.identificador}" />
                    </h:column>
                    <h:column>
                     <f:facet name="header">Nombre</f:facet>

                     <h:inputText id="nombres" value="#{user.u.nombres}"/>
                     <h:inputText id="apellidoP" value="#{user.u.apellidoPaterno}"/>
                     <h:inputText id="apellidoM" value="#{user.u.apellidoMaterno}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Active</f:facet>
                        <h:selectBooleanCheckbox id="check" value="#{user.u.active}"></h:selectBooleanCheckbox>
                    </h:column>

                </h:dataTable>

                <h:commandButton  value="Actualizar" type="submit" styleClass="btn-primary" actionListener="#{user.actualizarUsuario}">
                </h:commandButton>
 </h:form>
            <script type="text/javascript" src="js/paging-bootstrap.js"></script>
            <script type="text/javascript" src="js/contenidoc.datatable.init.js"></script>
        </h:panelGroup>
    </div>
</ui:composition>
user2018726
  • 638
  • 2
  • 12
  • 23

1 Answers1

5

Your concrete problem is caused because you used the wrong parameter names. Look in the generated HTML output and the HTTP traffic monitor for the right parameter names.

However, your actual problem is bigger: your view/model approach is completely wrong. You shouldn't be using a <h:dataTable> at all. It is intented for a collection of entities like List<User>, not for a single entity like User. You should be using <h:panelGrid>. You don't need to explode/flatten model properties in controller at all. You have those properties already in the model itself. You don't need to manually traverse the request parameter map. JSF will already do all the job for you.

I won't rewrite this mess for you, but to the point you should follow the following kickoff example:

Model:

public class User {

    private Long id;
    private String username;
    private String firstname;
    private String lastname;
    // ...

    // Autogenerate standard getters/setters.
}

Controller:

@ManagedBean
@ViewScoped
public class EditUser {

    private User user; // Initialize it in postconstruct or as viewparam.
    private UserService service; // Initialize it as @EJB or in postconstruct.

    public void save() {
        service.save(user); // That's all. Really.
    }

    public User getUser() {
        return user;
    }

    // No other getters/setters! They are all already in User class.
}

View:

<h:panelGrid>
    <h:inputText value="#{editUser.user.username}" />
    <h:inputText value="#{editUser.user.firstname}" />
    <h:inputText value="#{editUser.user.lastname}" />
    <h:commandButton value="save" action="#{editUser.save}" />
</h:panelGrid>

That's all. See also among others this JSF 2.0 tutorial. As to your attempt to get the user by ID, you should rather use <f:viewParam>, see also What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for? and communication in JSF 2.0.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thank u very much!!! i saw the correct names in the source code of the page and now it is working! i also changed the table for a panelgrid :) .. i thought that the "id" of the inputtext would work :S.. now i will try to use ! thanks again! – user2018726 Jan 30 '13 at 14:20
  • I want to repeat and stress that you **do not** need to traverse the request parameter map at all. You're unnecessarily overcomplicating things and taking over things which JSF already automatically does as being a MVC framework. You're basically completely overruling JSF and not utilizing its powers. You could as good go for plain JSP/Servlet this way. Given this construct, you need to change your code in such way that you don't need to grab `FacesContext` at all. You'll see that you end up with much smaller and simpler code. – BalusC Jan 30 '13 at 14:27
  • now i deleted all that not necessary trash (parameter map, etc), now my code is as simple as you told me.. wiii im happy lol! i feel like a potato for the stupid stuff i was doing. the only facescontext that is still there, is the one in the "init" with that one i get the userid from another view.. i gonna try to do that with . – user2018726 Jan 30 '13 at 14:31