Solved

Getting 'unchecked' value from Struts checkbox

Posted on 2004-08-23
10
1,174 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now