Solved

Getting 'unchecked' value from Struts checkbox

Posted on 2004-08-23
10
1,218 Views
Last Modified: 2007-12-19
I know that checkbox is only supposed to return a value IF checked but I'm getting a checked value returned all the time.  I'm using a DynaActionForm on a form that does a search i.e. you fill in 2 boxes at the top, click Search and the results are dsplayed below i.e. I can't just re-display the form nicely reset to default values.

When I uncheck the checkbox I expect there to be no reference to it in the dynaactionfom, yet there it is with a value of On!  Which means I cannot check for it correctly in the Action class.

How can I stop this please?

Thanks, Simon
0
Comment
Question by:ssherlock
  • 3
  • 3
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11871189
If you use a multibox like suggested here:

http://www.jguru.com/faq/view.jsp?EID=925277

then it returns an array of string that DOES match up to what was clicked...ie:  if it isn't selected, it isn't in the array...

Hope this helps...  checkboxes are a pain...  If this doesn't work, I should be able to find another way of doing it in my code somewhere ;-)

Tim
0
 

Author Comment

by:ssherlock
ID: 11871288
It seems overkill to try and use multibox for one field (and the article still mentions calling the reset method).  Do I need to look at somehow over riding the form (bearing in mind I'm using a DynaActionForm) that has a reset() method?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11871358
As you say, try resetting the checkbox value to false in the reset method of the form...

Hmmm..  I can't for the life of me remember how I fixed this...sorry...I'll have to dig back through my code...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
ssherlock earned 0 total points
ID: 11872861
It's so easy it's embarrassing!
Simply add a hidden field immediately after the checkbox set to false i.e.
<html:checkbox property="currentLocation" value="true"/>
<html:hidden name="currentLocation" value="false">

Struts (or maybe forms in general) reads the first line only.  If the checkbox is unchecked then the hidden field gets rid, otherwise the first one gets read.


Works a treat!
0
 

Author Comment

by:ssherlock
ID: 11872904
Of course it helps if you copy and paste the hidden field so the typos don't creep in :)
<html:hidden property="currentLocation" value="false"/>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11872927
;-)  Cool :-)

Glad you got it sorted :-)

(I must remeber to bookmark this page for next time I come up against that problem) ;-)

Hehehe

Tim
0
 
LVL 3

Expert Comment

by:Gunt
ID: 11876116
Although it works, be careful with that solution, it sounds fragile to me (two inputs with same name, only the first get processed, who knows if it's always that way, and if it's going to change in the future).
I suggest you check the code and identify how is it only the first found field with a name is being used.
Just an opinion to add, since you already figured it out.

Another solution (a little more verbose, but safer), is to create your dynaform class. Is very easy, you create a class and extend from DynaActionForm. I've done it to add lots of functionallity to the dynaform and works great (in that custom class you can then reset before submit or whatever you want to do).

Good luck.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 71
Opening PDF on button click and fill new document 2 53
Java 8 to Java 6 8 32
Java Inheritance super keyword use 8 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question