• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Struts - Check boxes problem

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Hope that any one can help me on this problem:
when the selected check boxes do not pass the edits, I return the same page with error message but the boxes user checked before submitting are unchecked.

What do I need to do to tell Strut to repopulate the check boxes?
Thanks

I have three check boxes coded in JSP as follows:
<html:checkbox property="checkedAction" value="printerCopyCCChecked"
title="Print Copy in CC" /><b>Print Copy in CC</b
<html:checkbox property="checkedAction" value="modifyClmtCopyChecked"
title="Modify & Reprint Claimant Copy" /><b>Modify & Reprint Claimant
Copy</b
<html:checkbox property="checkedAction" value="reprintClmtCopyChecked"
title="Reprint Claimant Copy" /><b>Reprint Claimant Copy</b
and My Action Form has:
private String [] checkedAction;
to store the value when user checks the check boxes.
[ October 02, 2007: Message edited by: Quang Pham ]
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to post Struts questions in the Struts forum. I have moved this post there for you.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have a set of checkboxes all with the same property name, you should use html:multibox instead of html:checkbox. If you do so, Struts will cause the boxes to show as checked or unchecked based on the value of the ActionForm property.
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. But our users want check boxes. Another question: How do I get Struts to call the reset method every time user click submit button? If I can do this, simply my problem is resolved. *** Currently I only able to call reset method only one at the time the Action Form constructor is invoked. ***
At first, I create different name for each check box. But Struts do not reset the Action Form check box field if I unchecked the box. When the box is checked, it stays checked due to Struts by pass calling set method for those unchecked boxes.
Later I use the same name for all check boxes. It solve the problem I got above but struts unchecked all the check boxes when return back to JSP. It is not appropriate to tell user "Invalid combination selection" with all check boxes are uncheked.
Thanks for your help
[ September 28, 2007: Message edited by: Quang Pham ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Quang Pham:
You are right. But our users want check boxes.


Both html:checkbox and html:multibox produce a checkbox. There is absolutely no difference in the way they appear on the screen. The only difference is behind the scenes in the way that Struts decides which box should appear checked and which unchecked.

Originally posted by Quang Pham:
How do I get Struts to call the reset method every time user click submit button?


You don't have to do anything. This is the Struts default behavior. Whenever the form is submitted, the reset method is called prior to populating the properties of the bean with its setter methods.

The problem you're experiencing has to do with the way HTML works with checkboxes. When it is unchecked, HTML does not submit any value when the form is submitted. To work around this, you have to put code in your reset method that will reset any values associated with the checkbox. In your example, you should put the following code in your reset method:

[ September 28, 2007: Message edited by: Merrill Higginson ]
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thank you very much. You are great.
Quang
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill
Not quite solve the problem. After check all boxes and submit, Struts does remember to populate the checked boxes. But then when I unchecked all of them, Struts bypass the set method and come back put the check on the selected boxes I submit the first time. Struts does not unchecked the boxes if all of them are unchecked. Any idea?
Quang
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the point I was trying to make in the second part of my previous post. HTML does not submit any value to the server if a box is unchecked, which means that your ActionForm's setter will not be called. The work around is to place the line

in your ActionForm's reset method. Also, make sure the method has the correct signature as defined in the JavaDoc.
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the check boxes field in reset method but the reset method never been called every time the submit button is pressed. The debugger never stop there. I think it may have some thing to do with the supper class my action form extends to. My action form class extends to our company ActionForm supper class which in turn extends to org.apache.struts.action.ActionForm;
Let me check with the frame work guy to see if the problem is in the supper class.
It's a good learning today.
Thank you again
Quang
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing you may want to check is the signature of your reset method. It must have exactly the following signature:
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill
OMG - The signature - That's it. It's is working.
Thank you.
Thank you.
Thank you.
Thank you.
Quang
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I used struts layout to populate my checkbox.

<layout:checkboxes key="flightallocation.location1" property="location" styleClass="LABEL" >
<layout: options collection="locationsList" property="data1" labelProperty="data2"/>
</layout:checkboxes>


'locationsList' having data from database.

It is populating correctly. And also when submitting the page I'm getting the values of selected locations in a array 'locationList'

when i want to update my records in database, I want to populate my records in the page. for example, If i have more than one location for the record, I fetch those locations, and put it in 'locationList' property in my form.

But i dont know how to populate these locations with selected checkboxes.
in my Page i'm having
'locationsList' having all the locations.it is a collection.
'locationList' having the location names to be selected. it is form bean property.it is ArrayList.

from these two collections, how can I populate my checkboxes with the selected locations?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roseline,

Please don't ask the same question in more than one thread. It wastes the time of those willing to give responses.

Those wishing to respond, please do so in this thread.
[ October 03, 2007: Message edited by: Merrill Higginson ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic