Building a Generic Bean

M

Mize-ze

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.
 
D

Daniel Pitts

Mize-ze said:
Mandar Amdekar ëúá:

Sounds complex,
Can you provide more information?
Thanks

I would suggest using a Map if you don't know the properties before
hand.

Or, if you are always going to have an XML document, just use the DOM
directly. There is nothing special about JavaBeans. As a matter of
fact, they are one of the most often abused convension.
 
D

Daniel Pitts

Mize-ze said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top