webtier@glassfish.java.net

Re: [webtier] can't update h:dataTable with List<String> as value

From: Joel Weight <digitaljoel_at_gmail.com>
Date: Tue, 3 Mar 2009 22:35:36 -0700

You can't "update" a string because String objects are immutable. This is
why it works with your StringWrapper and not with String. I'm a relative
newbie with facelets, but I'm guessing you'll have to use some object ( as
you already surmised with your StringWrapper ) that has a String property
that you can modify instead of changing the object itself like you are with
a List<String> and value="#{name}

Joel

On Tue, Mar 3, 2009 at 2:44 PM, <webtier_at_javadesktop.org> wrote:

> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>
>