rym
rym

Reputation: 11

How to create a group of checkbox with JSF?

I want to create a group of checkbox ,but the user have the right to check just 3, each checkbox represent a criteria of research in my database(eg :if user check checkbox by priority I should concat this search criteria to my request SQL). I have done some research in the internet but I have just found examples with ajax ,I dont want to use it.And I want to know should I use selectBooleanCheckbox OR selectManyCheckbox Is there a simple example?

Upvotes: 1

Views: 4138

Answers (2)

Jigar Joshi
Jigar Joshi

Reputation: 240860

JSF

<h:selectManyCheckbox value="#{myBean.values}">
    <f:selectItem itemValue="1" itemLabel="Value - 1" />
    <f:selectItem itemValue="2" itemLabel="Value - 2" />
    <f:selectItem itemValue="3" itemLabel="Value - 3" />
</h:selectManyCheckbox>

Bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="myBean")
@RequestScoped
public class MyBean{

    public String[] values;

        //getters setters

}

Update:

To print array values as mentioned in the example, you can have following util method

   public String getFavNumber3InString() {
    return Arrays.toString(favNumber3);
}

Upvotes: 2

Ken Chan
Ken Chan

Reputation: 90427

You should use selectManyCheckbox. And here is a simple example for you to follow

Upvotes: 1

Related Questions