Hi all, I've been researching how to handle arrays in DynaActionForms. In other words, situations where you need an array or list to represent rows of data on the screen and most especially when you don't know how any entries will be on the page. I looked around the web and found all sorts of hacks to get around the problem that struts does not initialise the arrays. Some people advocated inserting extra actions, loading stuff into session and all sorts of things. All of which sounded problematic, labour intensive and not really addressing the problem (No insults intended).
So I had a look at the struts code and came up with the following extension to the DynaActionForm. So far I've tested it using definitions of both a String array and LinkedList in my struts-config.xml and they have worked perfectly without requiring any extra actions or code to set them up. This version of the form basically extends arrays and lists on demand as the framework loads the data. Missing entries such as indexes 4 and 5 in my sample jsp are left as nulls.
Any comments welcome. I don't think I've doubled on anyones work, but let me know as I searched for solutions like this but didn't find any.
And a sample Jsp page:
And finally a sample action:
[ August 29, 2005: Message edited by: Derek Clarkson ]
Ahhh, the LazyDynaBean looks exactly like what I was looking for. But I cannote get it to work. I replaced my custom DynaForm with the LazyDynaBean but it keeps throwing errors at me I cannot figure out. The latest I got was after including the commons validator jar which it seemed to be looking for. Now I just get
Which means nothing to me. It's obviously something to do with the setup of the validator. But I'm not using it so I cannot see what it is looking for.
Any ideas ?
[ August 29, 2005: Message edited by: Derek Clarkson ] [ August 29, 2005: Message edited by: Derek Clarkson ]
posted 15 years ago
Ok got it working. I had to figure out the validator first which I had not used before or included in any projects. What a pain. It's real good and throwing out all sorts of vague messages that make no sense at all. But to summarise, the LazyDynaBean solution in the document looks good and as stated is a drop in replacement for the standard DynaActionForm Providing you have Validator Active !. However what it doesn't state is that in order to use this solution you also have to now amend the validation.xml every time you want to create a form that can handle arrays and lists.
Personally I'm wondering if this is over kill. I won't know until I investigate the Validator a bit more and decide if it's worthwhile or just the made creation of some engineer with no real world experience. I say this because sometimes the effort required to maintain solutions which break things up this much can be worse that coding it directly. Especially as projects get larger.