For something I’m making in Java/jsp, I’m going to have a lot of visual tables that look alike but for varying objects. Each of the objects has a different number of fields, but shares at least an id that links through a more detailed page of the object.
So I was thinking about the possibility of making something that I could reuse every time, no matter of how many fields the object has.
Here’s an example of what I’d like to achieve
Object A
-id
-name
-address
Object B
-id
-field2
-field3
Would like to translate that to:
Table for Object A's
id -- name -- address
1 Bert Street
2 Jeff Lane
Table for Object B's
id -- field2 -- field3
1 AB 5
2 Foo Bar
What would be the best way to achieve this? I was thinking of (other than the id), adding an enumerable getter to both objects with all the fields, then looping over that to generate the table.
Any better ways?
What I usually do is create a class like this to be my table model:
For every class I want to implement(in your case object A and object B) I override this Abstract model, something like:
I pass this object to the jsp, and then I have a generic jsp file for drawing the table:
All the code I posted should not be taken as a copy/paste solution, since it will probably not work right away. It should be taken as an idea, and a clean way of solving your issue.