3

I have jsf page:

....
<form jsfc="h:form" action="">
  <h:dataTable value="#{newMusician.strings}" var="preferredMusicGenre" id="musicGenresSelectTable">
    <h:column>
      <h:inputText value="#{preferredMusicGenre}" immediate="true"/>
     </h:column>
   </h:dataTable>
   <p>
      <input type="submit" jsfc="h:commandButton" value="Add" action="#{newMusician.saveNewMusician}"/>
   </p>
</form>
....

And managed bean that has ArrayList of Strings:

@ManagedBean
@ViewScoped
public class NewMusician {

    private ArrayList<String> strings = new ArrayList<String>();

    public NewMusician() {
        strings.add("olo");
    }
    public ArrayList<String> getStrings() {
        return strings;
    }
    public void saveNewMusician() {
    .....
    }
....
}

Problem: When I change text in and press save button, in saveNewMusician() method I can see that ArrayList "strings" contain the same old value "olo", but not that one I inserted in input field. The same problem if use h:selecOneMenu.

Situation is changed if use not string, but object that aggregate string and set value into string. So if I'll use some POJO and change inputText to:

<h:inputText value="#{preferredMusicGenrePojo.string}" immediate="true"/>

Everything becomes Ok.

Question: Why usage of 1 level getter <h:inputText value="#{preferredMusicGenre}"/> is incorrect, but usage of 2 level getter: <h:inputText value="#{preferredMusicGenrePojo.text}"/> is Ok?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MercurieVV
  • 404
  • 6
  • 14

1 Answers1

4

A String is immutable. It doesn't have a setter for the value. You need to wrap this around in a bean (or POJO as you call it).

public class Musician {
    private String preferredGenre; 

    // Add/generate constructor, getter, setter, etc.
}

Then change your managed bean as follows.

@ManagedBean
@ViewScoped
public class NewMusician {

    private ArrayList<Musician> musicians = new ArrayList<Musician>();

    public NewMusician() {
        musicians.add(new Musician("olo"));
    }

    public ArrayList<Musician> getMusicians() {
        return musicians;
    }

    public void saveNewMusician() {
        // ...
    }

    // ...
}

And your datatable:

<h:dataTable value="#{newMusician.musicians}" var="musician">
    <h:column>
        <h:inputText value="#{musician.preferredGenre}" />
    </h:column>
</h:dataTable>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for answer. Now that situation is clear for me, but actually it is siplified case. Original case was with POJO object. JSF was: "" and it was converter with custom converter. Can you help me with this case? – MercurieVV Jan 07 '11 at 18:31
  • This makes no sense as well. Fix it as per the example in my answer. – BalusC Jan 07 '11 at 18:33
  • Yes. But if I'm working with JPA model and do not want to wrap its entities. Or it is bad practice - combine JPA model in JSF Backing Bean? – MercurieVV Jan 07 '11 at 21:00
  • No, that's perfectly normal. But the JPA model has in turn also fields/properties, right? You need to get/set them individually by input elements, not the entire model by a single input element with help of some Converter. – BalusC Jan 07 '11 at 21:02
  • Thanks a lot. You save the day. – MercurieVV Jan 07 '11 at 21:59