带有动态列表名称的<s:select>

air*_*ump 3 java select jsp struts2 ognl

我想迭代一个包含<s:select>列表源名称的字符串列表,但HTML输出不是预期的:它是显示的列表的名称,而不是内容.

我的Action代码:

public class DescriptionTabArchiveAction extends ActionSupport {
    private List<String> vegetables = new ArrayList<String>();
    private List<String> devices = new ArrayList<String>();

    // contain "vegetables" and "devices".
    private List<String> selectList = new ArrayList<String>();

    @Action("multipleSelect")
    public String multipleSelect() {
                vegetables.add("tomato");
                vegetables.add("potato");

                devices.add("mouse");
                devices.add("keyboard");

                selectList.add("vegetables");
                selectList.add("devices");

        return SUCCES;
    }

       // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

JSP:

<s:iterator value="selectList" var="listName">

    <s:select list="%{#listName}" />

    <!-- I tried with this line too : same behaviour. -->
    <%-- <s:select list="#listName" /> --%>
</s:iterator>
Run Code Online (Sandbox Code Playgroud)

我得到了什么(html输出):

<select name="" id="">
    <option value="vegetables">vegetables</option>
</select>
<select name="" id="">
    <option value="devices">devices</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我期待什么(html输出):

<select name="" id="">
    <option value="tomato">tomato</option>
    <option value="potato">potato</option>
</select>
<select name="" id="">
    <option value="mouse">mouse</option>
    <option value="keyboard">keyboard</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我的问题:

如何动态迭代字符串列表以使多个<s:select>具有不同的列表源?

Rom*_*n C 5

用a Map代替List

private Map<String, List<String>> selectMap = new HashMap<>();
//getter and setter here

@Action("multipleSelect")
public String multipleSelect() {
    vegetables.add("tomato");
    vegetables.add("potato");

    devices.add("mouse");
    devices.add("keyboard");

    selectMap.put("vegetables", vegetables);
    selectMap.put("devices", devices);

    return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

修改迭代器以使用地图

<s:iterator value="selectMap">    
    <s:select list="%{value}" />
    ...
</s:iterator>
Run Code Online (Sandbox Code Playgroud)