1

I have a JSP in which I am iterating over a list and creating an editable table.

<s:iterator value="test" id="countryList">
  <tr>
    <td><s:textfield name="countryCode" value="%{countryCode}" theme="simple" /></td>
    <td><s:textfield name="countryName" value="%{countryName}" theme="simple" /></td>
  </tr>
 </s:iterator>

My list:

List<Country> test = new ArrayList<Country>();  

Country class:

public class Country {
    private String countryCode;
    private String countryName;


and the getter setters......

and say I populate the list like:

Country country = new Country();
Country country1 = new Country();

country.setCountryCode("1");
country.setCountryName("India");

country1.setCountryCode("2");
country1.setCountryName("US");

test.add(country);
test.add(country1);

Now when I change the values of countrycode and countryname in the JSP, I should get the updated values in my action. But am I not able to. Is there any way to get these updated values.

Roman C
  • 49,761
  • 33
  • 66
  • 176
DDas
  • 425
  • 6
  • 11

1 Answers1

2

You need the Country object is created when the params interceptor is populating your list using indexed property access.

<s:iterator value="test" id="countryList" status="stat">
  <tr>
    <td><s:textfield name="countryList[%{#stat.index}].countryCode" value="%{countryCode}" theme="simple" /></td>
    <td><s:textfield name="countryList[%{#stat.index}].countryName" value="%{countryName}" theme="simple" /></td>
  </tr>
 </s:iterator>

To let the struts populate the list you should use a type conversion annotation or equivalent in the xml configuration.

@Element(value = Country.class)
@Key(value = String.class)
@KeyProperty(value = "countryCode") 
@CreateIfNull(value = true)
private List<Country> countryList = new ArrayList<Country>;
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    It works!!!Thanks Romans...thanks a lot...saved my day. Was a nice thing to learn – DDas Oct 03 '13 at 09:53
  • Great, now you have to code to compare lists and update values. But you didn't save the previuos list, you need to fetch it again if you don't mind to hardcode values in the action. Better put it in the session because actions are recreated each time you make a request. – Roman C Oct 03 '13 at 09:56