Re: Building a Generic Bean
Mize-ze wrote:
Ok, this is complicated...
Can I build a bean which I can't know the names and number of fields it
will have from advance?
I thought about two general solutions:
1) Use a list or a map in the bean (which is ugly)
2) Use some sort of reflection like to build a bean that explicitly has
these fields (Can it be done at all?)
An example:
Let's say I have an XML file... and I need to build a web-form based on
that XML and to access the form using a form specific javaBean.
Now, the XML (and only the XML) defines the number and names of the
fields in the form. I have to build beans to handle the form processing
that I do not know the field names or number that they should hold.
The best solution I could come up with is to extract the fields from
the XML , then instantiate a bean that holds a Map and fill it with the
field names, then access the bean from the web (JSP) and extract the
field names to generate the form output. after submission I would set
the values entered by the user into the Map and send it away.
But with this solution I can never use the bean qualities of set/get
and have to use code to access the bean fields. It will be cumbersome
and ugly.
How would YOU solve it?
Thanks.
I know I've already replied in this thread, although I didn't read this
post as carefully as I should have.
The solution I would choose is just to use a Map (don't even bother
with a bean at all). Although, if I know more about the nature of your
XML semantics, I might be able to give a better solution, such as
avoiding the XML, and using Annotations, or some other approach.