aspose file tools*
The moose likes Struts and the fly likes Managing dynamic form elements in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Managing dynamic form elements in Struts" Watch "Managing dynamic form elements in Struts" New topic
Author

Managing dynamic form elements in Struts

Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
Hi,

I am having a requirement where jsp page can have variable number of elements(textbox). The value of these elements is required to pass to the database. Since number of textboxes are not fixed, i can not create a form bean for the same.
How can i create a dynamic form bean which will manage dynamic form elements.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Check DynaActionForm may be it will help you.
Even in DynaActionForm you should know property names in advance.
Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
Thanks Gowher.
As the elements are getting generated dynamically (based on record count obtained from db), we don't have any idea of the count of elements, hence we can't use Dyna Form here.

Awaiting for your valuable suggestions.
[ March 14, 2008: Message edited by: Saurabh Gupta ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You will need to use indexed properties for this. For more information on indexed properties along with a working example, see question 7 of this forum's FAQ


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Sounds like a case for Indexed Properties. There is an entry in the FAQ that includes some links and an example that I put together. Once you get the hang of indexed properties they are pretty easy to crank out but it took me a lot of trial and error to get them working the first time.

http://faq.javaranch.com/java/IndexedProperties

- Brent
Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
Merrill and Brent, Thanks for the suggestion.
I tried the shopping cart code given in the example.
I am getting following exception:

java.lang.NullPointerException
com.kmpr.forms.struts.action.FormsMasterUpdateAction.execute(FormsMasterUpdateAction.java:47)

This exception is coming when i am trying to get value of ArrayList in my Action class.
As per my understanding, the form bean object while displaying values and the object while retrieving value from bean are different (as i tried to print the object's hash code in Displayaction and UpdateAction).

Below is my JSP


Bean name:-Myform
Form Bean Name:- FormsMasterSearchForm

Following are the contents of FormsMasterSearchForm



Please help to resolve the issue.
Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
The exception i am getting is at the time of submitting the page after updating contents.
The display page is coming properly.
I have also verified everything as per your notes.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I don't understand why you used a plain HTML input tag instead of a struts html:text tag as was used in Brent's sample. The techniques given in the exmaple simply won't work unless you use the Struts tag. Given how your form bean is coded, your text field should look something like this:

Note that in any Struts html:xxx tag, the styleId attribute translates to an id attribute when the HTML is rendered.
[ March 15, 2008: Message edited by: Merrill Higginson ]
Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
Thanks to all.
It is working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Managing dynamic form elements in Struts