File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts html:checkbox problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts html:checkbox problem?" Watch "Struts html:checkbox problem?" New topic

Struts html:checkbox problem?

Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Hello all

I am facing a problem with struts <html:checkbox>.

I want to check or uncheck them according to my condiontions and also want to
assign some values like 1,2 etc.

I am handling these two things as follows:

1) To check or uncheck them i am using the following code :

Here "test" is my Actionform property which will return either true or false. And based on this values checkboxes will be checked or unchecked.
I have done upto this.
2) My second problem is the main problem for me.
I also want to assign these checkboxes some values like 1,2 etc.
for this i am using another ActionForm property as
String[] values;
Along with its getter and setters methods.
And in my logic iterate tag i am doing like this:

If for value i write like
<html:checkbox property="test" value="ActionForm property i.e values"/>
It gives me exception.
Anybody has any idea .
Thanks a lot for reading.

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Generally, when you have a bunch of related checkboxes that all have the same property name, it's best to use html:multibox instead of html:checkbox. They both produce checkboxes that look the same on the page, but Struts handles them differently.

If UserPermissions is a list or array of Strings, you could write the following:

When the form is submitted, "test" will contain an array of strings with one element for each permission that was checked.

Consultant, Sima Solutions
Khushwinder Chahal
Ranch Hand

Joined: Oct 03, 2007
Posts: 128
Thanks for your reply Merrill

But i have also tried multiboxes like

But in this case i am getting a problem ...which is as follows:
If i give name="userpermissions" then values are coming fine but all the
multiboxes become checked but if i donot give name="userpermissions" in the tag <html:multibox property="ids" name="userpermisions"> it will not check any of the checkboxes.
Thats why i shifted to <mtml:checkbox>.
Please suggest something.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
One thing that's important to understand is that you need two collections or arrays: One to contain the list of possible values for the checkboxes and another to contain the values actually selected by the user, and they must not be the same array. Since you've tried to make them the same array, that's why it's not working. You need to have a separate String array to collect the checked values, and it must be empty when the page is first displayed (unless you want all the boxes checked by default, in which case it should contain an array of all possible values).

Your code should look something like this:

This assumes that "someOtherVariableName" is a property of type String[] in your ActionForm bean.
subject: Struts html:checkbox problem?
It's not a secret anymore!