Question on binding attributes from objects from list.
Hello,
Scenario:
I have a list, say phone list. List holds phone objects. Phone has attribs like area code, exchange code, extension etc.
I have a jsf jsp, having a form that can take upto 3 phone numbers as input (3 input boxes per phone number). Please note I am not using datatable.
The mapping I have is something like this:-
#{phoneList[1].phone.areaCode}
Now this works fine if phone list is never empty. But I get array index exception when list has less than 3 phones. I worked around by always making sure that the list has 3 phone objects.
Problem:
I want to change the code that populates the phone list to be able to store any numbers of phone objects. Which means I cannot create a list that always has 3 or less dummy phone objects. How do I work this around?
Any input is appreciated.
-bchinc
# 2
> Why don't you use a datatable? This is perfectly
> suitable to display a list of objects.
I do not have dynamic display here. I mean I always have to show 3 input controls for 3 phones on my form.
What I have is a single form to display phone numbers and then allow update/insert/delete phone numbers. I didn't need datatable because I always created list with 3. I was the only user of the phone list.
Now I will be providing same code to another application that doesn't display anything. Its an IVR application. And there is no restriction on the number of phones that it can handle. So I cannot populate dummy phones in list. At the same time I cannot change my existing jsp code to display phone input controls dynamically.
# 5
Yes. Although navigation steps aren't required at all.
Datatables are nice, check http://balusc.xs4all.nl/srv/dev-jep-dat.html how you can use them.
One hint in your direction:
<h:dataTable value="#{myBean.phones}" var="phone">
<h:column>
<f:facet name="header"><h:outputText value="Area code" /></f:facet>
<h:inputText value="#{phone.areaCode}" />
</h:column>
<h:column>
<f:facet name="header"><h:outputText value="Exchange code" /></f:facet>
<h:inputText value="#{phone.exchangeCode}" />
</h:column>
...
</h:dataTable>
<h:commandButton value="add" action="#{myBean.addPhone}" />
MyBeanprivate List<Phone> phones; // + getter + setter
public void addPhone() {
phones.add(new Phone());
}
When clicking add, the new phone will show up as a new row with empty fields (or with default values, if any).