dcsimg
How to create dynamic form elements and receive them in the ActionForm
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gez_Hall
Posted On:   Wednesday, January 22, 2003 11:05 AM

Ok, I'm a struts newbie, so this may be a simple one or it may not...we shall see. I have my jsp page, with a couple of simple form elements (text:html). When submitted, the associated Action class creates an object of which the form values become attributes. The object is added to a Vector. The original page is then displayed. So, everytime the user enters some info and submits, one more object is added. In addition, once these objects are created, the attributes of them are displayed in the page as a list. So, say I was creating books, there would be a list of three (say) books, and a textfield at the bottom to create a new title. What I want to do, is make the list elements part of the form, by enclosing them    More>>

Ok, I'm a struts newbie, so this may be a simple one or it may not...we shall see.



I have my jsp page, with a couple of simple form elements (text:html). When submitted, the associated Action class creates an object of which the form values become attributes. The object is added to a Vector. The original page is then displayed. So, everytime the user enters some info and submits, one more object is added.



In addition, once these objects are created, the attributes of them are displayed in the page as a list. So, say I was creating books, there would be a list of three (say) books, and a textfield at the bottom to create a new title.



What I want to do, is make the list elements part of the form, by enclosing them within an input type (text:html). But these are dynamic - my ActionForm class doesn't know if they exist, how many of them there will be etc, or what they will be called. The list is displayed in a (logic:iterate) tag, so I can't even give them different names (ie. (text:html property="aValue")) - they would all be called 'aValue'.



I hope this makes sense, and that someone out there knows how I can get around this, any help would be really appreciated.



Thanks.

   <<Less

Re: How to create dynamic form elements and receive them in the ActionForm

Posted By:   Jerome_Iffrig  
Posted On:   Friday, January 24, 2003 04:59 AM

I believe that your problem is not a real problem - It comes from the fact, as you said, your are a newby in Struts, no big deal


This can be done by using an iteration over the Vector of your list of books.


Struts comes with handy tags to iterate over such objects: As long as the Vector has elements (in this case the vector would contain bean objects of type bookBean for instance), the iterate tag would repeat the piece of JSP code (the code that would display the elements contains by bookBean) it enclosed referring automatically, each time it loops, to the next object in the Vector.


You don't need to know in advance how many objects the Vector contains -


As for the name of the object, as this seems to be your main problem, it is up to you to set it within the bookBean object (e.g. this bean would contain a String named "name" that you would set to the name of a given book this bean corresponds to).


I am not quite sure I answered you question, but in any case, have a look at the WebApps delivered with Struts (unpack the WAR example archives it contains, you will get from them simple applications ready to run as well as their source code for you to understand the way they work) – Starting by this will give you a good kick.


Good luck.


Jérôme.

About | Sitemap | Contact