struts2

Go To StackoverFlow.com

0

I am using two tags.Both of them have same name, but different Id

<s:doubleselect id="countryId1" name="country"     list="countriesMap.keySet()" doubleId="cityId1"                                     doubleName="city" doubleList="countriesMap.get(top)" />

<s:doubleselect id="countryId2" name="country"     list="countriesMap.keySet()" doubleId="cityId2"                                     doubleName="city" doubleList="countriesMap.get(top)" />

In Action I tried to get

country String[] countryArray = ServletRequest.getParameterValues("country"); 

But I am getting countryArray = null. I looked at the page code and found such situation

<select name="country" id="countryId1" onchange="countryId1Redirect(this.options.selectedIndex)">
    <option value="USA">USA</option>
    <option value="Germany">Germany</option>
</select>

I selected value USA, but there is no selected='selected' property.

How can I put selected values from each <select name... into Array?

2012-04-03 20:50
by zhake
Don't forget to accept the answer if it helps. : - Mike Partridge 2012-04-03 21:13


1

To get a list of values from elements with the same name, create getters and setters matching the name; for example:

public class MyAction extends ActionSupport {

    private List<String> countries;
    private List<String> cities;

    public String execute() {

        if (getCountry() != null && getCity() != null) {
            for (int i = 0; i < getCountry().size(); i++) {
                System.out.println("country"+(i+1)+"="+getCountry().get(i));
                System.out.println("city"+(i+1)+"="+getCity().get(i));
            }
        }

        return SUCCESS;
    }

    // setCountry matches country
    public void setCountry(List<String> countries) {
        this.countries = countries;
    }
    public List<String> getCountry() {
        return countries;
    }

    // setCity matches city
    public void setCity(List<String> cities) {
        this.cities = cities;
    }
    public List<String> getCity() {
        return cities;
    }

}

I believe you can use String[] instead of List<String> if you like.

I don't have a way to test this at the moment, but you can use the index property of the status variable to get the iteration index, possibly something like this:

<s:iterator value="country" status="stat"> 
    <s:property /> <!-- the country -->
    <br />
    <s:property value="#city[#stat.index]" /> <!-- the city corresponding to the current country -->
    <br />
    <br />
</s:iterator> 
2012-04-03 21:10
by Mike Partridge
getters and setters won't work return data in XWorkList format. - zhake 2012-04-04 07:43