Problems getting selected values from HtmlSelectManyCheckbox

Hello,

I am having problems getting values via getSelectedValues() from my HtmlSelectManyCheckbox component. The method returns an object array but the values are the same as the initial selectItems property.

JSP:

<h:selectManyCheckbox id="checkBox"

binding="#{userEditBean.manyCheckbox}"

value="#{userEditBean.groupIds}"

layout="pageDirection">

<f:selectItems value="#{userEditBean.allGroups}"/>

</h:selectManyCheckbox>

<h:commandButton action="#{userEditBean.ok}"value="OK"/>

Bean:

publicclass UserEditBeanextends BaseBean{

private HtmlSelectManyCheckbox manyCheckbox =new HtmlSelectManyCheckbox();

private String[] groupIds={};

private List allGroups =new ArrayList();

public String[] getGroupIds(){

groupIds= backing.getSomeIds();

return groupIds;

}

}

publicvoid setGroupIds(String[] ids){

this.groupIds= ids;

}

public HtmlSelectManyCheckbox getManyCheckbox(){

return manyCheckbox;

}

publicvoid setManyCheckbox(HtmlSelectManyCheckbox newC){

this.manyCheckbox = newC;

}

public List getAllGroups(){

allGroups = backing.getAllGroups();

return allGroups;

}

publicvoid setAllGroups(List newList){

this.allGroups = newList;

}

public String ok(){

Object o = manyCheckbox.getSelectedValues();

}

The problem is, the getSelectedValues() returns the values from the initial state of the page. The is no mark of the changes made by the user. Any help will be appreciated

Thanks,

Matek

[3006 byte] By [Matek_ja] at [2007-11-27 7:33:36]
# 1

Your approach is somewhat odd. The selected values are just reflected in the groupIds, but you're overridding it with backing.getSomeIds() each time when the getter is called.

Here is a basic working example:<h:selectManyCheckbox value="#{myBean.selectedItems}">

<f:selectItems value="#{myBean.selectItems}"/>

</h:selectManyCheckbox>

<h:commandButton value="submit" action="#{myBean.action}" />

MyBeanprivate List<String> selectedItems;

private List<SelectItem> selectItems;

// + getters + setters

// You can use initialization block or constructor to prepopulate the selectItems.

{

selectItems = new ArrayList<SelectItem>();

...

}

//or

public MyBean() {

selectItems = new ArrayList<SelectItem>();

...

}

public void action() {

// The selected items are reflected in selectedItems.

for (String selectedItem : selectedItems) {

System.out.println(selectedItem);

}

}

BalusCa at 2007-7-12 19:14:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks for the fast answer BalusC, that solved my problem.
Matek_ja at 2007-7-12 19:14:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...