3

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("[email protected]");
        emails.add("[email protected]");
        userList.add(new User("User1", (List<String>) emails.clone()));

        emails.clear();
        emails.add("[email protected]");
        emails.add("[email protected]");
        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
  • Are you not able to see the email list or what ? Commented Oct 16, 2013 at 6:48

1 Answer 1

5

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Will it work for any the type inside ArrayList?
Well if your input's value is bound to something else than String, you'll need a converter.
Ok thanks for the answer, just one last doubt. What If I have List of BigDecimal types?
Just updated my answer to show how to use the BigDecimal converter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.