Re: Dynamic number of form fields
There was a good tutorial on this possibly linked from the struts page titled Monkey Struts. I found it to be quite good, very easy to follow. David Graham wrote: I want to have a form with a variable number of input boxes. For example, a simple edit person form that has a first name and last name input box for each person in your db. So the first set would be named firstName_12 lastName_12, the second set might be firstName_45623 and lastName_45623. The number after the underscore is the personID from the db table. When the form is submitted you loop over these fields and update the appropriate row in the database from the _ID info. I've read through the docs on DynaBeans and that seems like the way to go but I'm still not sure how to go about this. When populating the DynaFormBean, does struts just go through the request params and assign them to new form properties? How would you validate this type of form? Advice and links to info are appreciated. Thanks, Dave _ Join the world's largest e-mail service with MSN Hotmail. http://www.hotmail.com -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
On 9/11/2002 at 11:00 AM David Graham wrote: I want to have a form with a variable number of input boxes. For example, a simple edit person form that has a first name and last name input box for each person in your db. So the first set would be named firstName_12 lastName_12, the second set might be firstName_45623 and lastName_45623. The number after the underscore is the personID from the db table. When the form is submitted you loop over these fields and update the appropriate row in the database from the _ID info. I've read through the docs on DynaBeans and that seems like the way to go but I'm still not sure how to go about this. When populating the DynaFormBean, does struts just go through the request params and assign them to new form properties? How would you validate this type of form? The Dyna beans don't exactly give you what you're looking for. The properties of a Dyna bean must be known at deployment time, do they're not directly suited for dynamic forms (the Dyna prefix is a bit misleading). What you probably want are map-backed ActionForm beans (which can be vanilla or Dyna, actually, with some caveats). There is no documentation in the user guide for them, but I've written a patch that adds some. Comments on it are greatly welcomed. - donald patch Description: Binary data -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
Brilliant! Thanks so much for your help! That's a lot more elegant than what I had been doing, although it does create interesting dilemmas with javascript. Hopefully that patch will make it into the docs soon. Thanks, Dave From: Donald Ball [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: Dynamic number of form fields Date: Wed, 11 Sep 2002 13:34:18 -0400 On 9/11/2002 at 11:00 AM David Graham wrote: I want to have a form with a variable number of input boxes. For example, a simple edit person form that has a first name and last name input box for each person in your db. So the first set would be named firstName_12 lastName_12, the second set might be firstName_45623 and lastName_45623. The number after the underscore is the personID from the db table. When the form is submitted you loop over these fields and update the appropriate row in the database from the _ID info. I've read through the docs on DynaBeans and that seems like the way to go but I'm still not sure how to go about this. When populating the DynaFormBean, does struts just go through the request params and assign them to new form properties? How would you validate this type of form? The Dyna beans don't exactly give you what you're looking for. The properties of a Dyna bean must be known at deployment time, do they're not directly suited for dynamic forms (the Dyna prefix is a bit misleading). What you probably want are map-backed ActionForm beans (which can be vanilla or Dyna, actually, with some caveats). There is no documentation in the user guide for them, but I've written a patch that adds some. Comments on it are greatly welcomed. - donald patch -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
I'm having some trouble getting this to work. I get the error message javax.servlet.jsp.JspException: No getter method for property categoryName(2) of bean org.apache.struts.taglib.html.BEAN I have a HashMap in my form called categoryNames and a getter/setter like this: public void setCategoryName(String key, Object name) { this.categoryNames.put(key, name); } public Object getCategoryName(String key) { return this.categoryNames.get(key); } I put a bunch of AccountCategory objects in the session and then the jsp loops through to display the input boxes like this: c:forEach var=category items=${sessionScope.accountCategories} html:text property=categoryCode(%= ((AccountCategory)pageContext.getAttribute(category)).getAccountCategoryID() %)/ /c:forEach Any ideas? Do I need struts 1.1 for this (I'm using 1.0.2)? Thanks, Dave From: Donald Ball [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: Dynamic number of form fields Date: Wed, 11 Sep 2002 13:34:18 -0400 On 9/11/2002 at 11:00 AM David Graham wrote: I want to have a form with a variable number of input boxes. For example, a simple edit person form that has a first name and last name input box for each person in your db. So the first set would be named firstName_12 lastName_12, the second set might be firstName_45623 and lastName_45623. The number after the underscore is the personID from the db table. When the form is submitted you loop over these fields and update the appropriate row in the database from the _ID info. I've read through the docs on DynaBeans and that seems like the way to go but I'm still not sure how to go about this. When populating the DynaFormBean, does struts just go through the request params and assign them to new form properties? How would you validate this type of form? The Dyna beans don't exactly give you what you're looking for. The properties of a Dyna bean must be known at deployment time, do they're not directly suited for dynamic forms (the Dyna prefix is a bit misleading). What you probably want are map-backed ActionForm beans (which can be vanilla or Dyna, actually, with some caveats). There is no documentation in the user guide for them, but I've written a patch that adds some. Comments on it are greatly welcomed. - donald patch -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: Dynamic number of form fields
Correct me if I wrong, I think in the bean it should be: void setCategoryName(Map categoryName) and Map getCategoryName() -hemanth -Original Message- From: David Graham [mailto:[EMAIL PROTECTED]] Sent: Wednesday, September 11, 2002 3:54 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: Dynamic number of form fields I'm having some trouble getting this to work. I get the error message javax.servlet.jsp.JspException: No getter method for property categoryName(2) of bean org.apache.struts.taglib.html.BEAN I have a HashMap in my form called categoryNames and a getter/setter like this: public void setCategoryName(String key, Object name) { this.categoryNames.put(key, name); } public Object getCategoryName(String key) { return this.categoryNames.get(key); } I put a bunch of AccountCategory objects in the session and then the jsp loops through to display the input boxes like this: c:forEach var=category items=${sessionScope.accountCategories} html:text property=categoryCode(%= ((AccountCategory)pageContext.getAttribute(category)).getAccountCategoryID () %)/ /c:forEach Any ideas? Do I need struts 1.1 for this (I'm using 1.0.2)? Thanks, Dave From: Donald Ball [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: Dynamic number of form fields Date: Wed, 11 Sep 2002 13:34:18 -0400 On 9/11/2002 at 11:00 AM David Graham wrote: I want to have a form with a variable number of input boxes. For example, a simple edit person form that has a first name and last name input box for each person in your db. So the first set would be named firstName_12 lastName_12, the second set might be firstName_45623 and lastName_45623. The number after the underscore is the personID from the db table. When the form is submitted you loop over these fields and update the appropriate row in the database from the _ID info. I've read through the docs on DynaBeans and that seems like the way to go but I'm still not sure how to go about this. When populating the DynaFormBean, does struts just go through the request params and assign them to new form properties? How would you validate this type of form? The Dyna beans don't exactly give you what you're looking for. The properties of a Dyna bean must be known at deployment time, do they're not directly suited for dynamic forms (the Dyna prefix is a bit misleading). What you probably want are map-backed ActionForm beans (which can be vanilla or Dyna, actually, with some caveats). There is no documentation in the user guide for them, but I've written a patch that adds some. Comments on it are greatly welcomed. - donald patch -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
On 9/11/2002 at 1:02 PM David Graham wrote: Brilliant! Thanks so much for your help! That's a lot more elegant than what I had been doing, although it does create interesting dilemmas with javascript. Hopefully that patch will make it into the docs soon. Glad you found the docs useful. Out of curiousity, what javascript dilemma do you see using map-backed ActionForms creating? - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
On 9/11/2002 at 2:53 PM David Graham wrote: I'm having some trouble getting this to work. I get the error message javax.servlet.jsp.JspException: No getter method for property categoryName(2) of bean org.apache.struts.taglib.html.BEAN I have a HashMap in my form called categoryNames and a getter/setter like this: public void setCategoryName(String key, Object name) { this.categoryNames.put(key, name); } public Object getCategoryName(String key) { return this.categoryNames.get(key); } Looks correct to me. I put a bunch of AccountCategory objects in the session and then the jsp loops through to display the input boxes like this: c:forEach var=category items=${sessionScope.accountCategories} html:text property=categoryCode(%= ((AccountCategory)pageContext.getAttribute(category)).getAccountCategoryI D() %)/ /c:forEach Any ideas? Do I need struts 1.1 for this (I'm using 1.0.2)? You do need struts 1.1 for this, sorry, I should have made that explicit. Also, fwiw, I've had trouble with this usage: property=categoryCode(%= ((AccountCategory)pageContext.getAttribute(category)).getAccountCategoryI D() %)/ jasper doesn't seem to evaluate the %= % expression inside the prompt attribute value. Danged if I know why, but obviously my understanding of the jsp processing model isn't the best. I use a scriptlet instead: % String property = categoryCode( + foo + ); % html:text property=%=property%/ I would hope someone has a better solution to suggest. :) - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
I'm na new struts user, and I'm trying to get a true dynamic form to work. Could you give me more pointers on how to access this doc? Thanx, John - Original Message - From: Donald Ball [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 11, 2002 10:34 AM Subject: Re: Dynamic number of form fields The Dyna beans don't exactly give you what you're looking for. The properties of a Dyna bean must be known at deployment time, do they're not directly suited for dynamic forms (the Dyna prefix is a bit misleading). What you probably want are map-backed ActionForm beans (which can be vanilla or Dyna, actually, with some caveats). There is no documentation in the user guide for them, but I've written a patch that adds some. Comments on it are greatly welcomed. - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: Dynamic number of form fields
On 9/11/2002 at 3:56 PM Hemanth Setty wrote: Correct me if I wrong, I think in the bean it should be: void setCategoryName(Map categoryName) and Map getCategoryName() -hemanth I'm reasonably certain that you're wrong. The map-backed ActionForm parsing code doesn't actually presume that there's a Map backing the property, just something which acts like a map in that there exist these methods: void setCategory(String key, Object value); Object getCategory(String key); on the bean. The underlying data storage object could be essentially anything. Of course, I didn't write this code, just tried to document it, so I may be wrong. - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
Donald put this attachment on one of his posts. It's quite helpful and I've attached it again to this message. Dave From: John Averty [EMAIL PROTECTED] Reply-To: John Averty [EMAIL PROTECTED] To: Struts Users Mailing List [EMAIL PROTECTED],[EMAIL PROTECTED] Subject: Re: Dynamic number of form fields Date: Wed, 11 Sep 2002 14:47:18 -0700 I'm na new struts user, and I'm trying to get a true dynamic form to work. Could you give me more pointers on how to access this doc? Thanx, John - Original Message - From: Donald Ball [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 11, 2002 10:34 AM Subject: Re: Dynamic number of form fields The Dyna beans don't exactly give you what you're looking for. The properties of a Dyna bean must be known at deployment time, do they're not directly suited for dynamic forms (the Dyna prefix is a bit misleading). What you probably want are map-backed ActionForm beans (which can be vanilla or Dyna, actually, with some caveats). There is no documentation in the user guide for them, but I've written a patch that adds some. Comments on it are greatly welcomed. - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] _ Join the worlds largest e-mail service with MSN Hotmail. http://www.hotmail.com ? patch Index: doc/userGuide/building_controller.xml === RCS file: /home/cvspublic/jakarta-struts/doc/userGuide/building_controller.xml,v retrieving revision 1.23 diff -u -r1.23 building_controller.xml --- doc/userGuide/building_controller.xml 27 Jul 2002 21:53:13 - 1.23 +++ doc/userGuide/building_controller.xml 4 Sep 2002 15:36:28 - @@ -7,6 +7,7 @@ authorTed Husted/author authorMartin Cooper/author authorEd Burns/author +authorDonald Ball/author titleThe Struts User's Guide - Building Controller Components/title /properties @@ -90,11 +91,92 @@ /section section name=4.2.1 DynaActionForm Classes href=dyna_action_form_classes -p[:TODO:]/p + pMaintaining a separate concrete ActionForm class for each form in your struts application is time-consuming. This can be alleviated through the use of DynaActionForm classes. Instead of creating a new ActionForm subclass and new get/set methods for each of your bean's properties, you can list its properties, type, and defaults in the struts configuration file./p + pFor example, add the following to struts-config.xml for a UserForm bean that stores a user's given and family names:/p +pre +![CDATA[ +form-bean name=UserForm type=org.apache.struts.action.DynaActionForm + form-property name=givenName type=java.lang.String initial=John/ + form-property name=familyName type=java.lang.String initial=Smith/ +/form-bean +]] +/pre + pThe list of types supported by DynaActionForm beans includes:/p + ul +lijava.lang.BigDecimal/li +lijava.lang.BigInteger/li +liboolean and java.lang.Boolean/li +libyte and java.lang.Byte/li +lichar and java.lang.Character/li +lijava.lang.Class/li +lidouble and java.lang.Double/li +lifloat and java.lang.Float/li +liint and java.lang.Integer/li +lilong and java.lang.Long/li +lishort and java.lang.Short/li +lijava.lang.String/li +lijava.sql.Date/li +lijava.sql.Time/li +lijava.sql.Timestamp/li + /ul + pIf you do not supply an initial attribute, numbers will be initialized to 0 and objects to null./p /section -section name=4.2.3 Map-backed ActionForms href=map_action_form_classes -p[:TODO:]/p +section name=4.2.2 Map-backed ActionForms href=map_action_form_classes + pThe DynaActionForm classes offer the ability to create ActionForm beans at initialization time, based on a list of properties enumerated in the struts configuration file. However, many HTML forms are generated dynamically at request time. Since the properties of these forms' ActionForm beans are not all known ahead of time, we need a new approach./p + pStruts allows you to make one or more of your ActionForm's properties' values a Map instead of a traditional atomic object. You can then store the data from your form's dynamic fields in that Map. Here is an example of a map-backed ActionForm class:/p +pre +![CDATA[ +public FooForm extends ActionForm { + +private final Map values = new HashMap(); + +public void setValue(String key, Object value) { +values.put(key, value); +} + +public Object getValue(String key
Re: Dynamic number of form fields
On 9/11/2002 at 3:59 PM David Graham wrote: Javascript doesn't like it when you use things like [ or ( in the name of a form field. There are several ways of coding the js and one of them accepts those characters (I think). So, you can still use javascript to validate it but you might have to change the method by which you do it. Of course, when I get around to learning the struts validator stuff I won't need to do the js portion (hopefully :-). Oh, that's easy. instead of: form.foo.value use form.elements['foo'].value Regarding the validator... I don't know how easy it will be to make the struts validator stuff play with dynamic form inputs. I fear it will be tricky to impossible, since the validator assumes you can enumerate all of the properties that it will be validating in its configuration file. Perhaps a struts validator guru may have a better answer. - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
On 9/11/2002 at 3:20 PM John Averty wrote: Duuh! Sorry about that. I'd like to bring it one step further: I'd like to be able to set the corresponding field type. Would the following work? % for (int i=0; i10; i++) { String name = value(foo- + i + ); String type = type(foo- + i + ); html:%=type% property=%=name%/br/ % Almost certainly not. If you want to dynamically choose a different type of element, I think you basically need to fake a switch statement with a bunch of logic:equals elements. - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: Dynamic number of form fields
Yep, I think you're right. That was worth a try though ;-) Thanks, John. - Original Message - From: Donald Ball [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 11, 2002 3:24 PM Subject: Re: Dynamic number of form fields On 9/11/2002 at 3:20 PM John Averty wrote: Duuh! Sorry about that. I'd like to bring it one step further: I'd like to be able to set the corresponding field type. Would the following work? % for (int i=0; i10; i++) { String name = value(foo- + i + ); String type = type(foo- + i + ); html:%=type% property=%=name%/br/ % Almost certainly not. If you want to dynamically choose a different type of element, I think you basically need to fake a switch statement with a bunch of logic:equals elements. - donald -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]