小编典典

具有动态列表名称

jsp

我想遍历包含<s:select>列表源名称的字符串列表,但是HTML输出不是预期的: 它是显示的列表名称,而不是content

我的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
}

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>

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

<select name="" id="">
    <option value="vegetables">vegetables</option>
</select>
<select name="" id="">
    <option value="devices">devices</option>
</select>

我期望什么(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>

我的问题:

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


阅读 299

收藏
2020-06-08

共1个答案

小编典典

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

修改迭代器以使用地图

<s:iterator value="selectMap">    
    <s:select list="%{value}" />
    ...
</s:iterator>
2020-06-08