java - struts2 <s :doubleselect>

标签 java jsp select struts2

我使用两个标签。它们具有相同的名称,但不同的 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)" />

在行动中我试图得到

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

但我得到 countryArray = null 。我查看了页面代码,发现了这样的情况

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

我选择值USA,但是没有selected='selected'属性(property)。

如何从每个 <select name... 中放置选定的值进入数组?

最佳答案

要从具有相同名称的元素中获取值列表,请创建与名称匹配的 getter 和 setter;例如:

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;
    }

}

我相信你可以使用String[]而不是List<String>如果你愿意的话。

我目前没有办法对此进行测试,但您可以使用状态变量的索引属性来获取迭代索引,可能是这样的:

<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> 

关于java - struts2 <s :doubleselect>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10001182/

相关文章:

java - 属性文件不起作用

jsp - 在 JSP/JSPX : no solution for problem that should not even exist? 中转义 HTML 实体

function - 在 Vim 中删除整个函数定义

mysql - 在 Mysql 中使用 CASE 将值设置为别名字段

php - 代码点火器选择为

java - 包含 Java 中所有特定字母的正则表达式

java - 带 GSI 的 dynamodb 过滤器

java - 如何在更改单元格值后渲染单元格颜色Jtable

java - Jsp页面获取客户端IP地址

java - JTextField + 对话框的输入验证