How to Get the check box in html:checkbox automatically get checked on display .

I am using an int array to initialize the checkboxes when clicked. I want to display them as checked. I do not want to use javascript etc. Please suggest a way to get them checked. I am not using any boolean value in the bean rather its an integer array.

The above code assumes you have a property named picks of type int[] in your ActionForm. Prior to displaying this page, populate the picks property with some values and the correct values will be checked. For example, if picks contains "1, 2", Boxes one and two will be checked, while box three will be unchecked.

hi Merrill, Thanks for the reply. I got it working and i am getting the checkboxes as checked when the value matches. This involves hardcoding the values though.. HENCE this alone is not solvin my problem as the values are coming dynamically and assigning the formbean property with the values at declaration is not practical. I have the values (an arraylist in an object) with me in the request scope. I need to have a way to get these values somehow assigned to the bean variable. As far as i guess this code will go inside a JSP. I am not able to figure out as how do i do this in the JSP i.e. populating the bean variable (integer array) for checkbox with the values from an arraylist.

I typed up a quick response...and then I tried jumping over to Google and searching on the phrase "html:multibox example" and the very first hit was an example of almost exactly what I typed.

oh well...here is my example:

- Brent

Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 85

posted Nov 19, 2006 22:06:00

0

Hi Brent, I did not get how does this example is solving my problem. Please brief if possible.

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

posted Nov 20, 2006 00:38:00

0

I have the values (an arraylist in an object) with me in the request scope. I need to have a way to get these values somehow assigned to the bean variable. As far as i guess this code will go inside a JSP

This code should not go in a JSP, but in an Action class that forwards to this JSP. Just write code to copy the values from wherever they are into a String array within your Actionform.

suppose you have an ArrayList of MyBean objects, and that the id property of each MyBean object should constitute the list of which check boxes are checked and which are not. The following code would work:

In order to dynamically create a set of checkboxes, you would use code similar to the example that Brent gave.