File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Recurring fields in formbean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Recurring fields in formbean" Watch "Recurring fields in formbean" New topic

Recurring fields in formbean

Raymond Chiam

Joined: Apr 06, 2004
Posts: 2
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,

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
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 ]

Leandro Melo
SCJP 1.4, SCWCD 1.4
Raymond Chiam

Joined: Apr 06, 2004
Posts: 2
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.

Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
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.
I agree. Here's the link:
subject: Recurring fields in formbean
It's not a secret anymore!