Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Managing dynamic form elements in Struts

 
Saurabh Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check DynaActionForm may be it will help you.
Even in DynaActionForm you should know property names in advance.
 
Saurabh Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.
It is working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic