Need a best practice for showing a list with different objects in it.

Hello.

From my database layer I get a list with objects that all extend the same super object but every object in the list could be different. I need the jsf page to show each object in its own way depending on what type of class it is.

First I thought of coding the whole thing in the object it self. It could just return html for the page but there must be a smarter way. Perhaps to check what type of object Im working with on the jsf page and then include a different jsf page depending on what type the object is.

But how would you solve this?

Sincerely Ole Bille.

[600 byte] By [Cogos.dk_Olea] at [2007-11-27 9:44:55]
# 1

Most clean way is to create a superclass/interface with the desired methods and use them.

Another way is to depend the rendering of the components on the object type. But this would involve more (nasty) work and reduce maintainability.

Also see this thread: http://forum.java.sun.com/thread.jspa?threadID=5157064

BalusCa at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Not sure I understand what you mean by superclass/interface with desired methods and use them. Do you mean to make a toHTML() method on the objects and then have the objects create the html I want?
Cogos.dk_Olea at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Reading the other post. Seams like its the same problem =)
Cogos.dk_Olea at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> Not sure I understand what you mean by

> superclass/interface with desired methods and use

> them. Do you mean to make a toHTML() method on the

> objects and then have the objects create the html I

> want?

Uhm OK? I was talking in the OO perspective.

Create a superclass or superinterface and let those objects extend or implement it. Then pass List<Superclass> (or List<SuperInterface> of course) to the datatable and then use the methods as definied in the superclass or superinterface.

BalusCa at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Ahh hehe well I know how to use superclasses and interfaces that wasnt what I didnt understand :-) I just wasnt sure how you suggested to use it in this case. From what I can read in the post you linked me to is that I can check what type of object I have in the list and then render them differently. Like this

<h:dataTable value="#{myBean.list}" var="item">

<h:column>

<c:if test="#{item.class.name == 'mypackage.Cat'}'}" >

<!-- here I can render my cat -->

</c:if>

<c:if test="#{item.class.name == 'mypackage.Dog'}'}" >

<!-- here I can render my dog-->

</c:if>

<c:if test="#{item.class.name == 'mypackage.Fish'}'}" >

<!-- here I can render my fish-->

</c:if>

</h:column>

</h:dataTable>

Cogos.dk_Olea at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Rather use h:panelGroup instead of c:if. I don't know if you have tested it anyhow, but this it not going to work flawlessly in a datatable.
BalusCa at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thx alot BalusC for your help Ill give you points and consider this question closed :-)
Cogos.dk_Olea at 2007-7-12 23:52:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...