在ui:repeat中使用inputText更新值的ArrayList不起作用 [英] Using inputText inside ui:repeat to update ArrayList of values is not working

查看:81
本文介绍了在ui:repeat中使用inputText更新值的ArrayList不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Post的标题.,我正在尝试从ui:repeat内部的h:inputText更新ArrayList的值,这是行不通的.
请参阅以下模型以进一步了解:

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:

我有一个POJO类,如下:

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
}

在我的管理器中,我创建了用户POJOS列表:

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
}

现在在我的Facelet中,我正在使用ui:repeat将数据加载到表中的h:inputText中,以便用户可以编辑和更改值. 方面代码:

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>

当我编辑#{user.name}时上述方法可以正常工作,但不适用于#{email}.
我可以假定它适用于#{user.name},因为name具有setter和getter方法.
因此,如何更新emails列表对象.
我的POJO设计不好吗?还是使用ui:repeat是个坏主意?
我该如何实现?

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?

注意:我当前的Mojarra版本是2.1

推荐答案

正如BalusC报告的那样此处 String是不可变的

As BalusC reported here String is immutable.

使用varStatus属性可通过索引直接访问列表成员.

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>

使用BigDecimals:

With BigDecimals:

<ui:repeat varStatus="loop" value="#{user.numbers}">
    <td><h:inputText value="#{user.numbers[loop.index]}" converter="javax.faces.BigDecimal"/> </td>
</ui:repeat>

这篇关于在ui:repeat中使用inputText更新值的ArrayList不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆