Hello folks, Is there a character limit to these posts? If so I'm about to go over it... I'm hoping someone can let me know if I'm on the right track with something. Here's the scenario: I have an ActionForm subclass called PeopleForm that has on it an ArrayList called emails and an ArrayList called phones:
Email and Phone are themselves JavaBeans - (each ArrayList ultimately represents a one-many relationship in the database). Through what mechanism, for example, can I add a new Email object to the PeopleForm "emails" ArrayList at runtime - (for example by clicking on an "add email" button on a jsp that calls a method in a subclass of DispatchAction)? I guess the mental block I'm having can be summed up like this: Given a DispatchAction method called "addRelatedObject" that will be used by [bold]more than one[/bold] form:
How in this "save" method can I do something like...
When, at runtime I may only know through a request parameter (from an <html:hidden> field on the form) what type of object I need to create, but not what form I'm on and therefore not what ArrayList to add the new obejct to! For example:
What I was thinking of doing to solve the issues is writing a constant class that contains constants that map the objectType parameter to a particular object (I already have this class for other uses):
Then writing a helper class that has a method that is essentially a big switch/case statement (or just make it a private method of the action class):
The problem I'm having (you may have noticed that I have lots of them) is determining what form the request is being submitted from so I can cast my ActionForm to the right type and call the right method. Because the action is being used by more then one ActionForm bean, I need to make sure that calling getEmails() the form isn't going to throw a NowSuchMethodException or whatever it is they throw.... Concievably, I know that if the object type is "1" for Email, then the form being used has a getEmail() method... but what if I have two ActionForm classes that have a getEmails() method? Which one should I cast? Or should I make sure getEmails() is only declared on one ActionForm and any other forms that need getEmails() extend it? Sorry for the long-winded post... I thought a lot of this out while I was writing it, so that exercise has at least been helpful. Any feedback for a more elegant solution is appreciated. It's not that I don't think I can get the method described above to work, it's that it just doesn't seem SIMPLE enough.... so I'm hoping that STRUTS provides a better way that I don't know about... Thanks! --BW [ March 12, 2004: Message edited by: Brian R. Wainwright ] [ March 12, 2004: Message edited by: Brian R. Wainwright ]