• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example:



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.
 
Satish Kumar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brent,
I did not get how does this example is solving my problem.
Please brief if possible.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
He does not suffer fools gladly. But this tiny ad does:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic