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?