Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why input (for example h:inputText) nested in h:dataTable do not update Bean model? [duplicate]

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?

like image 827
MercurieVV Avatar asked Sep 14 '26 22:09

MercurieVV


1 Answers

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>
like image 119
BalusC Avatar answered Sep 17 '26 10:09

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!