get data from <h:selectManyCheckbox>

hi

iam trying this from so many days....can anyone tell me where iam going wrong

<html>

<body>

<f:view>

<h:form>

<h:selectManyCheckbox id="checkbox" layout="pageDirection" value="#{mybean.selectedItems}">

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

</h:selectManyCheckbox>

<h:commandButton action="#{mybean.someFunction}" value="getData" immediate="true"/>

</h:selectManyCheckbox>

</h:form>

</f:view>

</body>

</html>

bean

public class MyBean

{

private List selectItems=new ArrayList();

private List selectedItems=new ArrayList();

MyBean()

{

try

{

......

selectItems.add(..........................));

}

catch(Exception e)

{

System.out.println(e);

}

}

public List getSelectItems() {

return selectItems;

}

public void setSelectItems(List selectItems) {

this.selectItems = selectItems;

}

public List getSelectedItems() {

return selectedItems;

}

public void setSelectedItems(List selectedItems) {

this.selectedItems = selectedItems;

}

public String someFunction()// this method is not invoked at all.........

{

if (selectedItems!=null)

{

int sz=selectedItems.size();

System.out.println("size"+sz);

ListIterator it =selectedItems.listIterator();

while(it.hasNext())

{

String select=it.next().toString();

System.out.println("extra"+select);

}

}

return "success";

}

}

faces-congif.xml

<faces-config>

<managed-bean>

<managed-bean-name>mybean</managed-bean-name>

<managed-bean-class>org.MyBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

<navigation-rule>

<from-view-id>/index.jsp</from-view-id>

<navigation-case>

<from-outcome>success</from-outcome>

<to-view-id>/index1.jsp</to-view-id>

</navigation-case>

</navigation-rule>

</faces-config>

[2339 byte] By [honey17a] at [2007-11-27 3:28:57]
# 1
I want the data which is selected in checkboxes,but the selection is going off when the command button is pressed,and giving zero items selected....
honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Remove the immediate attribute and use it only once you fully understand it :)
BalusCa at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
hithanks ,i have removed immediate attribute....but still it is same
honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Then likely a conversion or validation error has been occurred. Add h:messages to the code to retrieve the error message.
BalusCa at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

hi

<f:view>

<h:form>

<h:panelGrid>

<h:selectManyCheckbox id="checkbox" layout="pageDirection" value="#{mybean.selectedItems}">

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

</h:selectManyCheckbox>

<h:message for="checkbox" showDetail="true" />

<h:commandButton value="getData" action="#{mybean.someFunction}" />

</h:panelGrid>

</h:form>

</f:view>

but where will the error messages be displayed.....

i dint nt found the error messages

honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
can you please give me a example of the <h:selectManyCheckbox> as iam unable to understand where am i going wrong.
honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
I had given an example in reply #3 here: http://forum.java.sun.com/thread.jspa?threadID=5166694
BalusCa at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
but iam uanble to find out where iam going wrong.
honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Here is a complete working snippet.

JSF<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<f:view>

<html>

<head>

<title>test</title>

</head>

<body>

<h:form>

<h:selectManyCheckbox value="#{myBean.selectedItems}">

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

</h:selectManyCheckbox>

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

</h:form>

</body>

</html>

</f:view>

MyBeanpackage mypackage;

import java.util.ArrayList;

import java.util.List;

import javax.faces.model.SelectItem;

public class MyBean {

private List<String> selectedItems;

private List<SelectItem> selectItems;

public void action() {

for (String selectedItem : selectedItems) {

System.out.println(selectedItem);

}

}

public List<SelectItem> getSelectItems() {

if (selectItems == null) {

selectItems = new ArrayList<SelectItem>();

selectItems.add(new SelectItem("value1", "label1"));

selectItems.add(new SelectItem("value2", "label2"));

selectItems.add(new SelectItem("value3", "label3"));

}

return selectItems;

}

public List<String> getSelectedItems() {

return selectedItems;

}

public void setSelectedItems(List<String> selectedItems) {

this.selectedItems = selectedItems;

}

}

BalusCa at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
hi BalusCthanks for the code.i will try accordingly and let you know soon abt it.
honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
hi BalusCthanks for the code.i will try accordingly and let you know soon abt it.
honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

hi BalusC

i tried ,but i got this error,what does it mean

javax.servlet.ServletException: Value binding '#{myBean.selectItems}'of UISelectItems with component-path {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /chekbox.jsp][Class: javax.faces.component.html.HtmlSelectManyCheckbox,Id: _idJsp0][Class: javax.faces.component.UISelectItems,Id: _idJsp1]} does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type : null

honey17a at 2007-7-12 8:31:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...