2013-04-29 40 views
3
<h:selectManyListbox id="sectorsListBox" size="2" multiple="multiple" value="#{Mybean.classificationSelectedItems}"> 
     <f:selectItems id="sectors" value="#{Mybean.classificationSelectItems}"/> 
</h:selectManyListbox> 

豆有:H:selectManyListBox二傳手沒有設置備份選擇的所有值

public class Mybean 
{ 
private Map<String,String> classificationSelectItems = new LinkedHashMap<String,String>(); 
private List<String> classificationSelectedItems = new ArrayList<String>(); 

//getter and setter for both. 
} 
init() 
{ 
classificationSelectItems.put("INS","Insurance") 
classificationSelectItems.put("HLC","HealthCare") 
} 

的選擇多框獲取與這兩個值進行初始化,但問題是隻有最後選擇的條目獲得存儲在classificationSelectedItems。爲什麼 ?我如何獲得存儲在classificationSelectedItems列表中的所有選定條目?

添加FYI,init方法是Spring的類。

+0

我剛剛測試過的例子,它工作正常(我使用List alter String []):http://www.mkyong.com/jsf2/jsf-2-multiple-select-listbox-example/ – 2013-04-29 01:23:23

+0

如果你選擇保險和醫療保險,都做在您的classificationSelectedItems設置? – Phoenix 2013-04-29 01:46:18

+0

你可以發佈代碼嗎?當然,他們兩個都是 – Phoenix 2013-04-29 01:47:13

回答

1

我曾與一個examle(參考:http://www.mkyong.com/jsf2/jsf-2-multiple-select-listbox-example/)測試,好運:)

的Facelets:

<h:form id="form"> 
     <h:selectManyListbox value="#{user.favFood1}" > 
      <f:selectItems value="#{user.favFood2Value}" /> 
     </h:selectManyListbox> 
     <h:commandButton value="test"/> 
    </h:form> 

豆:

@ManagedBean(name = "user") 
@ViewScoped 
public class UserBean implements Serializable { 

    private static final long serialVersionUID = 1L; 
    public List<String> favFood1; 
    private Map<String, Object> food2Value; 

    public UserBean() { 
     favFood1 = new ArrayList<String>(); 
     food2Value = new LinkedHashMap<String, Object>(); 
     food2Value.put("Food2 - Fry Checken", "Fry Checken1"); //label, value 
     food2Value.put("Food2 - Tomyam Soup", "Tomyam Soup2"); 
     food2Value.put("Food2 - Mixed Rice", "Mixed Rice3"); 
    } 

    public List<String> getFavFood1() { 
     return favFood1; 
    } 

    public void setFavFood1(List<String> favFood1) { 
     this.favFood1 = favFood1; 
    } 

    public Map<String, Object> getFavFood2Value() { 
     return food2Value; 
    } 
} 
+0

榮,我使用mojarra – Phoenix 2013-04-29 02:38:47

+0

我也是,我使用mojarra 2.1.21 :) – 2013-04-29 04:26:23

0

我注意到這正是行爲當我在設置方法中使用Collection,如

public void setClassificationSelectedItems(Collection<String> in){ 
    // store it somewhere 
} 

這二傳手是在恢復階段,但不會在更新階段調用,所以先前設定值將被設置,但從來沒有新的。如果使用List,它按預期工作:

public void setClassificationSelectedItems(List<String> in){ 
    // store it somewhere 
} 

請注意,你需要這樣的變化之後重新部署應用程序,因爲JSP需要重新編譯,但這不是自動完成的。