问题
As referring to Post's title., I'm trying to update the values of ArrayList from h:inputText inside ui:repeat, which is not working.
Please see the following mock up for further understanding:
I have a POJO class as follows:
public class User implements Serializable{
private String name;
private List<String> emails;
public User(String name, List<String> emails) {
super();
this.name = name;
this.emails = emails;
}
//Setters Getters
}
In my Manager I have created list of User POJOS:
@ManagedBean
@ViewScoped
public class UserManager implements Serializable {
private List<User> userList;
public UserManager() {
userList = new ArrayList<User>();
ArrayList<String> emails= new ArrayList<String>();
emails.add("user1.1@mail.com");
emails.add("user1.2@mail.com");
userList.add(new User("User1", (List<String>) emails.clone()));
emails.clear();
emails.add("user2.1@mail.com");
emails.add("user2.2@mail.com");
userList.add(new User("User2", (List<String>) emails.clone()));
}
public void action(){
for(User u : userList){
System.out.println(u);
}
}
//Setters Getters
}
Now in my Facelet I'm Using ui:repeat to load the data to h:inputText in to table, so that user can edit and change the values.
Facelet Code:
<h:form id="userForm">
<table border="1">
<ui:repeat var="user" value="#{userManager.userList}">
<tr>
<td><h:inputText value="#{user.name}"/> </td>
<ui:repeat var="email" value="#{user.emails}">
<td><h:inputText value="#{email}"/> </td>
</ui:repeat>
</tr>
</ui:repeat>
</table>
<h:commandButton value="Save" action="#{userManager.action}">
<f:ajax execute="@form @this"/>
</h:commandButton>
</h:form>
The above approach works fine when I edit the #{user.name} but its not working with #{email}.
I can assume the its working for #{user.name} because name has setter and getter methods.
So how do I update the emails list object.
Is my POJO design is poor? or Is it a bad idea to use ui:repeat?
How can I achieve this?
Note: my current Mojarra version is 2.1
回答1:
As BalusC reported here String is immutable.
Use the varStatus attribute to access directly the list member by the index.
<ui:repeat varStatus="loop" value="#{user.emails}">
<td><h:inputText value="#{user.emails[loop.index]}"/> </td>
</ui:repeat>
With BigDecimals:
<ui:repeat varStatus="loop" value="#{user.numbers}">
<td><h:inputText value="#{user.numbers[loop.index]}" converter="javax.faces.BigDecimal"/> </td>
</ui:repeat>
来源:https://stackoverflow.com/questions/19395621/using-inputtext-inside-uirepeat-to-update-arraylist-of-values-is-not-working