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

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

选择多个框使用这两个值进行初始化,但问题是只有最后一个选择的条目存储在分类选择项中。为什么呢 ?以及如何获取存储在 classificationSelectedItems 列表中的所有选定条目?

添加仅供参考,init 方法是 Spring 的类。

4

2 回答 2

1

我已经用一个例子进行了测试(参考:http ://www.mkyong.com/jsf2/jsf-2-multiple-select-listbox-example/ ),祝你好运:)

小面:

<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;
    }
}
于 2013-04-29T01:58:39.520 回答
0

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

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

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

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

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

于 2014-09-17T13:12:26.380 回答