How to make javax.faces.model.SelectItem selected

I am creating a javax.faces.model.SelectItem List (in a javax.faces.model.SelectItem ) for use with h:selectManyCheckbox , but I cannot figure out how to make the selected SelectItem .

How to do it? It should be possible, right? ...

  public List<SelectItem> getPlayerList(String teamName) { List<SelectItem> list = new ArrayList<SelectItem>(); TeamPage team = (TeamPage) pm.findByName(teamName); List<PlayerPage> players = pm.findAllPlayerPages(); for (PlayerPage player : players) { boolean isMember = false; if (team.getPlayerPages().contains(player)) { isMember = true; } SelectItem item; if (isMember) { // TODO: Make SelectItem selected??? item = null; } else { item = new SelectItem(player.getId(), createListItemLabel(player), "", false, false); } list.add(item); } return list; } 
+6
java jsf
source share
2 answers

Suppose we have this JSF code:

 <h:selectManyCheckbox value="#{bean.selectedValues}"> <f:selectItems value="#{bean.playerList}"/> </h:selectManyCheckbox> 

then the selected values ​​(i.e., checked flags) are stored in the bean.selectedValues ​​property.

So in your Java code, you have to handle selectValues ​​by putting the correct identifier in the selectedValues ​​property.

+9
source share

If someone works with selectOneMenu and dynamically populates the elements: When creating a SelectItem, if you provide some value (the first parameter) as follows:

 new SelectItem("somevalue", "someLabel", "someDescription", false, false); 

This translates to HTML:

 <option value="somevalue">someLabel</option> 

If you do not specify the following value:

 new SelectItem("", "someLabel", "someDescription", false, false); 

it translates as

 <option value="" selected="selected">someLabel</option> 

Therefore, if you want the item to be the default item when the page loads (for example, "Select one of them"), do not specify a value. If you create more than one element without a value, any of them is selected by default when the page loads (probably, preference is based on increasing alphabetical order).

-one
source share

All Articles