This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recurring fields in formbean

 
Raymond Chiam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have this problem here that I think can and should be more gracefully handled.

Lets say we have a form that requires the user to key in details for his family members. Details for each member are likely to be name, address, age, DOB etc.

Our STRUTS formbean can cater to that simply by hardcoding all the fields.

private String name;
private String DOB;
private String age;

However in order for details to be entered for more than 1 person, the form bean will have to look something like this: -

private String name1;
private String name2;
private String name3;
private String DOB1;
private String DOB2;
private String DOB3;
private String age1;
private String age2;
private String age3;

This will still work, but is really NOT object oriented.
And it will also bring other design problems along with it.
What if the user can choose to enter the number of family members he has before specifying their details?
E.g. he can choose 3 members and the form will display only 3 sets of fields for him to fill in.

In that case, how many sets of fields must we hard-code in our form bean?

I am sure there are better ways, perhaps through xml or writing custom tag libraries. Just hope that someone could enlighten me if such extension already exist in STRUTS.

Thanks and regards,

Raymond
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts accepts arrays, so, just go with

private String[] names;

Keep in mind though that the visibility is another problem. How many fiels should you display, is user allowed to input how many fiels he/she wants, etc... are question that you might handle depending on your business logic.
[ September 06, 2004: Message edited by: Leandro Melo ]
 
Raymond Chiam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leandro,

Thanks for the prompt reply.
Maybe I should have posted my question this way.
Can the above mentioned problem be solved in a more Object Oriented way?

For example we have a Person class that has the attributes pertaining to each individual as such: -

class Person{
private name;
private addr;
private gender;

public getName(){...}
public getAddr(){...}
public getGender(){...}

...
}

and in the STRUTS form, we can have many Person objects.

class MyFormBean extends ValidatorActionForm {
Person myPerson1 = new Person();
Person myPerson2 = new Person();
Person myPerson3 = new Person();
}

Such method allows me to first populate the business objects inside MyFormBean and then display the information to my JSP.
However after capturing the user entered data, the MyFormBean captured in the submitted request does not feature newly entered data.

I was wondering if there is any way to capture the newly entered data WITHOUT hardcoding all the person attributes or use arrays.

Thanks
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raymond,

You could try using "Indexed Properties" for accessing say a List of Person objects.

I have not personally tried this out, but it seems possible.
Check out this link for more on Using Indexed Properties in Form Beans.

Hope this helps.
Sheldon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic