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

[888 byte] By [bchinchalkara] at [2007-11-27 0:44:32]
# 1
Why don't you use a datatable? This is perfectly suitable to display a list of objects.
BalusCa at 2007-7-11 23:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

bchinchalkara at 2007-7-11 23:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
You're just limited by bad design. Redesign it by removing the limit and introducing the datatable.
BalusCa at 2007-7-11 23:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
I think you are right. I too thought that its a bad design to always show 3 input controls when there can be less or more than 3 phones. If I have to show only available phone numbers, then I have to make update/ insert as additional navigation steps. am I getting it right?
bchinchalkara at 2007-7-11 23:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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).

BalusCa at 2007-7-11 23:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Yup!! made perfect sense !! Thanks for your post.Now I have to convince this to business people. Hope they would appreciate!!
bchinchalkara at 2007-7-11 23:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...