This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts html:checkbox problem?

 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
And I DONOT WANT TO USE SIMPLE HTML CHECK BOX.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic