Phoenix
Phoenix

Reputation: 8923

h:selectManyListBox setter not setting all the values selected

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

Backing Bean has:

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

The select many box gets initialized with these 2 values but the problem is only the last selected entry is getting stored in classificationSelectedItems. Why is that so ? And how do I get all the selected entries stored in the list of classificationSelectedItems ?

Adding FYI, the init method is class by Spring.

Upvotes: 3

Views: 3817

Answers (2)

Matthias Ronge
Matthias Ronge

Reputation: 10112

I noticed exactly this behaviour when I used a Collection in the setter method, like

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

This setter is called during the restore phase but not during the update phase, so the previously set value will be set, but never the new one. If you use a List, it works as expected:

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

Note that you will need to redeploy the application after such a change because the JSP needs to be recompiled but this isn’t done automatically.

Upvotes: 0

Rong Nguyen
Rong Nguyen

Reputation: 4189

I have tested with an examle(reference:http://www.mkyong.com/jsf2/jsf-2-multiple-select-listbox-example/), good luck :)

Facelets:

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

Bean:

@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;
    }
}

Upvotes: 1

Related Questions