2016-09-23 17 views
3

我遇到了Struts2的問題。我有一個HashMap<String, Integer>,我用一個選擇的形式來選擇名稱和相應的名稱。如何在選擇Struts2中選擇HashMap的值?

HashMap迭代並使用名稱填充我的<select>列表。我只需要一個ID來挑選。我如何選擇一個ID /值而不是名稱/鍵?

companyMap is a HashMap,where <String, Integer> or key,value。

companyName是一個字符串。

<s:select label="Pick the company name" 
    headerKey="-1" headerValue="Select Company name" 
    list="companyMap.keys" 
    name = "companyName" 
/> 

回答

3

如果列表是一個Map(key,value),那麼Map鍵將成爲選項'value'參數,Map值將成爲選項主體。

但你需要更改這個規則

<s:select label="Pick the company name" 
    headerKey="-1" headerValue="Select Company name" 
    list="%{companyMap.entrySet()}" 
    name = "companyId" 
    listKey="value" 
    listValue="key" 
/> 
+0

謝謝!交換「價值」和「鑰匙」確實奏效。 – RomZes13