webtier@glassfish.java.net

can't update h:dataTable with List<String> as value

From: <webtier_at_javadesktop.org>
Date: Tue, 03 Mar 2009 13:44:56 PST

Hi,

I have an h:datatable which is bound to a List<String> object in my bean class. I want the datatable to display a list of inputs which will allow the user to update these String values.

My problem is that my bean is not accepting the user's updates. If I create a new object to hold the String (i.e. StringWrapper), and I attach my data table to a List<StringWrapper>, I get the functionality I want. But do I really have to resort to this?

Any input or ideas appreciated. Thanks.

Here is my code:

[b]Facelet:[/b]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:a4j="http://richfaces.org/a4j"
        xmlns:f="http://java.sun.com/jsf/core">
<head>
</head>
<body>
<f:view>
        <!-- This datatable is dealing with an object, it works fine -->
        <h:form>
                <h:dataTable id="people" value="#{testObj.list}" var="person">
                        <h:column>
                                <h:inputText value="#{person.firstname}">
                                        <a4j:support event="onchange" reRender="rows" />
                                </h:inputText>
                        </h:column>
                </h:dataTable>
                <h:panelGroup>
                        <h:commandButton value="Add Row" action="#{testObj.addPerson}" />
                </h:panelGroup>
        </h:form>
        <!-- This datatable is dealing with String, it doesn't work at all -->
        <h:form>
                <h:dataTable id="names" value="#{testObj.names}" var="name">
                        <h:column>
                                <h:inputText value="#{name}">
                                        <a4j:support event="onchange" reRender="names" />
                                </h:inputText>
                        </h:column>
                </h:dataTable>
                <h:panelGroup>
                        <h:commandButton value="Add Row" action="#{testObj.addName}" />
                </h:panelGroup>
        </h:form>
</f:view>
</body>
</html>

[b]Backing Bean:[/b]
public class TestObj implements Serializable {

  private static final long serialVersionUID = 0x066E639ED3098D82L;

  private List<Person> people = new ArrayList<Person>();
  private List<String> names = new ArrayList<String>();

  public TestObject() {
      people.add(new Person());
      names.add(new String());
  }

  public List<Person> getList() {
      return people;
  }

  public String addPerson() {
      people.add(new Person());
      return null;
  }
  
  public List<String> getNames() {
    return names;
  }
  
  public String addName() {
    names.add(new String());
    return null;
  }
  
  public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    private String firstname;
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

}
}
[Message sent by forum member 'chrisouellette' (chrisouellette)]

http://forums.java.net/jive/thread.jspa?messageID=334921