2013-12-13 103 views
3

我想遍歷包含<s:select>列表源名稱的字符串列表,但HTML輸出並不如預期:這是顯示,而不是內容列表的名稱。<s:select>動態列表名稱

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>不同列表源?

+0

嘗試填寫選擇的屬性的其餘部分。 [選擇標記文檔](http://struts.apache.org/release/2.0.x/docs/select.html) – Dan

+0

@Dan:我通過刪除無用標記來簡化問題,以關注問題。 – airdump

回答

5

使用的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> 
+0

thx!有用。 – airdump