user617597
user617597

Reputation: 786

How can I create struts form class when I'm not sure about the number of input fields

I have two tables, country_list, and rating_list. There is seperate jsp, to add new country to the system. The problem is with the Edit ratings page in which all the ratings for the countries can be edited. When I open the page Edit ratings, all the country names will be displayed one by one as label with corresponding text field to give the rating the user want to give. When the user click submit on the edit ratings page, how the ratings values can be mapped to form value? the country name list can be populated from the database, but what about the ratings? If I'm sure about the number of country then I can given that many rating fields in the form class. But I'm not sure how many text fields will appear. I'm using struts 1.2. Do I need to create like 100 strings with empty values in the form though it is not a good practice. Is this is the only way, any other alternatives?

For example, for the login page I can create the two fields, like "String username;String password" and correspongding setters and getters for the same.

But in my problem, I'm not sure how many countries and corresponding ratings. so I;m counfused if I give the country name as list and ratigns as list, while submitting the form , will the country names and ratings automatically mapped to the corresponding list.

Upvotes: 0

Views: 547

Answers (3)

Martin Wilson
Martin Wilson

Reputation: 3386

You might find the following technique useful. In your form class add a getter and setter similar to these:

public void setRating(String a_CountryId, String a_sRating)
{
    // Store the rating for the given country id, e.g. in a map
}

public String getRating(String a_sCountryId)
{
   // Return the rating for the given country id, e.g. from a pre-populated map
}

You can then access these using the following syntax in the JSP page:

property="rating(${country.id})"

For example, iterate over the countries in the JSP page and use an html:input tag to tie to the above getter, e.g. something like this:

<logic:iterate name="countryList" id="country">
    <bean:write name="country" property="name"/>
    Rating:
    <html:text name="form" property="rating(${country.id})"/>
</logic:iterate>

Upvotes: 1

mprabhat
mprabhat

Reputation: 20323

No you dont have to create 100's of String.

Say you have a list of Object like

List<Country> countryList;// This list is getting populated say from database

Your jsp code will have something like this:

<logic:iterate name="countryList" id="countryListId">
  <label><bean:write name="countryListId" property="countryName"/></label>
</logic:iterate>

As per comment:

Considering Country looks like this:

public class Country {

  private String countryName;

  private List<Rating> ratingList;

}

Your bare minimal html can look like this:

<table>
    <logic:iterate name="countryList" id="countryListId">
      <tr>
          <td>
              <label><bean:write name="countryListId" property="countryName"/></label>
          </td>
          <logic:iterate id="ratingListId" name="rating" property="ratingList">
              <td>
                  <input type="text"><bean:write name="ratingListId" property="ratingName"/></input>
               </td>
          </logic:iterate>
      </tr>
    </logic:iterate>
</table

Upvotes: 0

Th0rndike
Th0rndike

Reputation: 3436

use the logic tags, in particular logic:iterate. Here's a link to how it works:

struts logic:iterate

Upvotes: 0

Related Questions