0

I have an application that stores users in a database. I want that administrators can upate the data of those users and tried to create a dataTable that calls a bean to get a list of users and iterates over the users to output their data. I output the data in inputText fields so that the administrator is able to edit their data. Next to the inputText fields is a commandLink to save the changes, which should call a bean-method (sessionScoped) to merge changes into the database.

I tried to do this with a dataTable and also manually with a foreach. Neither of them worked.

The problem is that in the bean-method that is called when the link is clicked, the values are still the old ones and not the edited ones (I want it to be the new values from the inputText fields). I did some research an read about the actionListener. So I added an actionListener to the commandLink that is supposed to copy the new values of the inputText fields to a bean-attribute, but it didn't work either.

This is the code of my jsf-page:

<c:forEach items="#{UserBean.getUserList()}" var="customer">
      <tr>
          <h:form>
              <td>
                  <h:inputText disabled="true" value="#{customer.username}"/>
              </td>
              <td>
                  <h:inputText value="#{customer.firstname}"/>
              </td>
              <td>
                  <h:inputText value="#{customer.lastname}"/>
              </td>
              <td>
                  <h:inputText value="#{customer.email}"/>
              </td>
              <td>
                  <h:commandLink action="#{UserBean.update()}" value="Save changes">
                      <f:setPropertyActionListener target="#{UserBean.user}" value="#{customer}" />
                  </h:commandLink>
              </td>
          </h:form>
      </tr>
 </c:forEach>

This is my bean:

@ManagedBean(name="UserBean")
@SessionScoped
public class UserBean implements Serializable{

    private Users user;

    private List<Users> userList;


    public UserBean() {
        user = new Users();
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public String update(){
        System.out.println(user.getFirstname());

        return null;
    }

    public List<Users> getUserList(){
        userList = findAll();
        return userList;
    }

    public void setUserList(List<Users> userList){
        this.userList = userList;
    }

    public List<Users> findAll(){
        if(userSessionBean.getSessionUser().getUgroup() > 1){
            userList = em.createNamedQuery("Users.findAll")
                    .getResultList();
            return userList;
        }
        else{
            userList = em.createNamedQuery("Users.findById")
                    .setParameter("id", userSessionBean.getSessionUser())
                    .setMaxResults(1)
                    .getResultList();
            return userList;
        }
    }
}

Does anyone know what I do wrong or what the problem could be?

Marc
  • 257
  • 1
  • 7

1 Answers1

2

You're loading the model in the getter method.

public List<Users> getUserList(){
    userList = findAll();
    return userList;
}

So everytime when the getter is called, it will return a fresh new model and override any changes previously done on the model.

Move that line to the bean's (post)constructor method.

@PostConstruct
public void init() {
    userList = findAll();
}

public List<Users> getUserList(){
    return userList;
}

Once you've fixed it, just use a <h:dataTable>. This results in less boilerplate code.

See also

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much. Now it works. But I don't get it working with the tag, because I don't know where to put the tag? I tried to put it before the command link, but that doesn't work, because then the inputTexts are not in and so it doesn't take the new values. When I place it after the tag, then nothing is shown on the page (no link, not text-fields). When I place it before the tag all the values of all users are in the form, which is not appropriate, because I want to change just one user. With the c:foreach I can place it before every row. – Marc Feb 19 '12 at 15:06