h:selectManyListBox setter未设置所有选定的值

时间:2013-04-29 00:49:16

标签: jsf

<h:selectManyListbox id="sectorsListBox"  size="2" multiple="multiple" value="#{Mybean.classificationSelectedItems}">
      <f:selectItems id="sectors" value="#{Mybean.classificationSelectItems}"/>
</h:selectManyListbox>

Backing Bean有:

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")
}

使用这两个值初始化select many框,但问题是只有最后选择的条目存储在classificationSelectedItems中。为什么会这样 ?如何将所有选定的条目存储在classificationSelectedItems列表中?

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

2 个答案:

答案 0 :(得分:1)

我已通过考试测试(参考: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;
    }
}

答案 1 :(得分:0)

当我在setter方法中使用Collection时,我注意到了这种行为,例如

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

在恢复阶段调用此setter,但在更新阶段不调用此setter,因此将设置先前设置的值,但不会设置新值。如果您使用List,它会按预期工作:

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

请注意,您需要在更改后重新部署应用程序,因为需要重新编译JSP,但这不是自动完成的。