A. Cusano
A. Cusano

Reputation: 87

Struts2 - Dynamic form fields and database data retrieval

I am developing a registration web app using Struts2 and need some guidance.

Background:

On the registration form, there is a set of five form fields: 1 text box, and 4 drop down select boxes. The five fields describe a person's primary position in an educational setting: the text field allows the user to insert their job title, and the drop down menus allow the user to select what school, institution, department, and division they belong to. The drop down menus are initialized with options that are stored in a database (inside the registration action, array lists are initialized with these values before the form is displayed). For example:

<s:select emptyOption="true" key="school1.schoolId" list="schoolList" listKey="schoolId" listValue="schoolName" required="true" />

Problem:

I need to provide the user with the ability add an X number of secondary positions. On the registration form, a user can click an "add another affiliation" button, and a new set of the 5 form fields are displayed. These fields will also need to be validated, and saved when the user clicks the form's submit button.

What would be the best approach to tackling this problem?

So far, I have only declared array lists for each form field, like so:

private List<String> jobTitles = new ArrayList<String>();
private List<School> schools = new ArrayList<School>();
private List<Institution> institutions = new ArrayList<Institution>();
private List<Department> departments = new ArrayList<Department>();
private List<Division> divisions = new ArrayList<Division>();

But I do not know how to proceed. How do I display the initial 5 fields for the primary position? If I use Javascript to insert new form fields dynamically, how do I initialize the dynamic drop down menus with the options stored in the database? How do I retain these values if the page is reloaded?

Any help is appreciated - thanks!

Upvotes: 0

Views: 7220

Answers (1)

Asa
Asa

Reputation: 1729

The basic problem you need to tackle is how to get an indexed list of request parameters into your action class. This is quite simple, and I think you are on the right track by starting off by creating Lists of input parameters. I found a bit of documentation on the subject here. Basically you can have form fields with names like jobTitles[0], jobTitles[1] which would be used to populate the jobTitles List.

However, I think the concept of 'Affiliation' deserves a class of it's own:

class UserAffiliation {
   private String title;
   private String schoolId;
   private String institutionId;
   private String departmentId;
   private String divisionId;

   // Make sure that there is a no-args constructor (default or explicit) for Struts to create instances. 
   // Add getters and setters
}

In your action class:

   private List<UserAffiliation> affiliations;
   ...
   // getter and setter for affiliations

Would be enough to capture the user input.

Your jsp could look something like:

<form action=".." method="post">
  <div class="affiliation">
     <s:textfield name="affiliations[0].title"/>
     <s:select name="affiliations[0].schoolId" list="schools" listKey="schoolId" listValue="schoolName"/>
     ...
  </div>

  <s:if test="affiliations != null && affiliations.size > 1">
    <s:iterator value="affiliations" begin="1" status="status">
        <s:textfield name="affiliations[%{#status.index + 1}].title"/>
        <s:select name="affiliations[%{#status.index + 1}].schoolId" list="schools" listKey="schoolId" listValue="schoolName"/>
            ...
    </s:iterator>
  </s:if>
  ....
</form>

<div id="affilationTemplate" style="display:none;">
   <div class="affiliation">
      <s:textfield name="affiliations[__IDX__].title"/>
      <s:select name="affiliations[__IDX__].schoolId" list="schools" listKey="schoolId" listValue="schoolName"/>
   </div>
   ...
</div>

Note the div affilationTemplate. You could use JS to get the html of this template, replace __IDX__ with the appropriate index, and append to the form contents when the user clicks on the 'add another affiliation' button. This makes sure that the newly added select boxes are pre-populated with appropriate values.

The iterator block displays what ever the values the user had already submitted (with the exception of the 'primary affiliation', which is already displayed above it).

NOTE: You should of course, try to get rid of the repeated form elements if possible. I would try with extracting them into an include.

Upvotes: 1

Related Questions