问题描述
我想遍历包含 <s:select> 列表源名称的字符串列表,但 HTML 输出与预期不符:这是列表的名称是显示,而不是内容.
I want to iterate over a list of strings containing names for <s:select> list source, but the HTML output is not as expected : it's the name of the list that is display, not the content.
我的操作代码:
My Action code:
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>?
How can I dynamically iterate over a list of string to have multiple <s:select> with different list source?
推荐答案
使用 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>