Question

I am displaying information from a List<String> on the UI inside editable textboxes using <a4j:repeat>. The user can click on a button on the UI to add mmore textboxes and add values into them (using an ajax call). My problem is that I am unable to set the value the user enters back into this List<String>

Below is the view

<a4j:repeat value="#{implMemoMBean.objA.objB.objC.listBillNumber}" var="item">                      
    <h:inputText class="list-bill"  value="#{item}" />                                          
</a4j:repeat>

The code in the bean to add when the user clicks on an 'Add' button is as follows

objA.objB().objC().getListBillNumber().add("");

This adds a String to the List<String> and rerenders the UI, which then displays an extra textbox. The value entered in this textbox is not being set on postback.

How is this caused and how can I solve it?

Was it helpful?

Solution

The String class is immutable and doesn't have a setter for the value. There's nothing like String#set(newString) or so. The getter is basically the Object#toString() method.

You need to get/set the value directly on the List by loop index instead. You can use the rowKeyVar attribute to define the EL variable name of the current loop index.

<a4j:repeat value="#{implMemoMBean.objA.objB.objC.listBillNumber}" rowKeyVar="i">                       
    <h:inputText class="list-bill" value="#{implMemoMBean.objA.objB.objC.listBillNumber[i]}" />                                           
</a4j:repeat>

Note that you don't need a setter for the listBillNumber. EL will get the item by List#get(index) and set the item by List#add(index,item).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top