wood burning stoves 2.0*
The moose likes Struts and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark ""checked" property of a html:checkbox field ...  ?" Watch ""checked" property of a html:checkbox field ...  ?" New topic
Author

"checked" property of a html:checkbox field ... ?

Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
a classic html box can be designed "checked" if we want .......
<checkbox name="checkName" value="wow" checked >
there is a trick to have in video a checkbox created with <html:checkbox ... tag or <html:multibox ... tag checked ?!
( the struts doc it's quite clear, there is not any "checked" or "unchecked" property , only "disable" )
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Whether it is checked or not is determined by the associated property.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
<c:forEach items="${allUsers}" var="smsUser">

<html:checkbox property="selectedItems" value=
"<%= ((SmsUser)pageContext.getAttribute(\"smsUser\")).getIsActive()%>" />
</c:forEach>
i iterate a collection of objects SmsUser type....
getIsActive() return various values true or false of any single object , but that not mean that in video i obtain checked or unchecked checkboxes .....
"property" - "Name of the request parameter that will be included with this submission, set to the specified value "( Struts doc citation )
that mean is the value that goes in submit if i check the box , but it seems that in video it's ever unchecked ....
do you have some working example ?! ( with multibox it's ok too )
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
property is the name of the property from the form used to determine whether the box is checked or not. value is the value that is returned to your program when the box is checked. You shouldn't need value for most applications.
See this:
http://www.fawcette.com/javapro/2003_01/online/servletsjsp_bkurniawan_01_17_03/default_pf.asp
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
:-) nope ......your link seems to confirm what i said... anyway , not even a simple
<html:checkbox property=
"notify"/>
or
<html:checkbox property=
"notify" value="on"/>
doesn't make your check to be checked when you go for the first time in the page .... ( already tested it )
and neither an pure html
<input type="checkbox" name="notify" value="on">
doesn't desgin it checked if you don't specify it like this :
<input type="checkbox" name="notify"
value="on" checked>
- "value" it's the value that the checkbox get AFTER submitting the form, but it seems there is no struts equivalent for html option "checked"
!?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Whether it is checked or not is determined by the associated property.

If I want the "save info" checkbox checked then all I need to do is this in my form:

Since I defined boolean saveInfo = true;
and saveInfo is the associated property, the box will be checked when the form is displayed.
[ March 05, 2003: Message edited by: Thomas Paul ]
[ March 05, 2003: Message edited by: Thomas Paul ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

This is a nasty one. There's no HTML construct for UNchecked on a checkbox!
Struts gets around this by using the Form Bean. in the associated Form Bean's reset() method you should ALWAYS pre-initialize the boolean value that goes with the checkbox. reset() gets called when the form bean is first constructed. Thereafter the form bean is kept up to date by the normal Action Processors which use the item's is/get methods to reference and/or change it. Struct itself manages bean updates coming back from the client.
reset() may also be called explicity if you need to clear out a form bean for some reason.
BTW ONLY the reset() method can be used to initialize a form bean property for a checkbox. static or constructor initialization won't work properly!


Customer surveys are for companies who didn't pay proper attention to begin with.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Hmmm. Doesn't reset() get called each time a request is processed by the Struts Action Servlet (as opposed to when the form bean is constructed)?
My understanding is that the servlet calls the reset() method prior to setting form bean properties via a call to BeanUtils.copyProperties() or some such method. The base implementation of reset() does nothing, thus the form bean fields will appear to keep their values between requests if you don't override it in your ActionForm.
The reason a checkbox is tricky is that when you don't check it in the HTML form, the parameter doesn't come in with a request. If the form bean property associated with the checkbox was already true, it wouldn't be set to false by BeanUtils because there is no corresponding request parameter. It is therefore necessary to set any form field associated with a checkbox to false in the reset method. If the request parameter is present, BeanUtils will set it to true.
To complicate the issue, if the Action is called from different JSPs (e.g. "wizard"-type input pages), you have to test which page the current request is associated with; you don't want to reset the property if the request was submitted from a page that didn't have the checkbox.


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Wally,
I think I have an idea of what you're asking. If you have in your JSP
<html:checkbox property="gender" value="M" />
<html:checkbox property="gender" value="F" />
The taglib takes care of adding "checked" to the appropriate checkbox based on the value of the gender form field. That is, the first checkbox will be checked if the form method getGender() returns "M"; the second will be checked if getGender() returns "F".
Edit: or maybe I've confused this with radio buttons. Anyway, try it out and see. I can't test anything until tomorrow.
[ March 05, 2003: Message edited by: Junilu Lacar ]
Wally Schnok
Greenhorn

Joined: May 31, 2001
Posts: 29
Hi , thomas, your example it works , but you're using a static initilization in your form...
i need to assigg a value that can be true or false but returned by an object ......
because i have a list o checkbox , with various values ( true of false ) in base of an object
in fact the question is :
can i assing in my property a component of an object ?
i mean, something like this :
<html:checkbox property="myObect.myProp" />
where myObj is the instance of a bean and myProp an element of it
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The property is always tied back to the Form bean. You can update the variable in the Form bean based on anything you like but the property in the HTML tag has to tie back to the Form bean.
Notice the recommendation to use the reset() method from the previous posters.
[ March 06, 2003: Message edited by: Thomas Paul ]
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
Also check out the html multibox tag. That's come in handy many times for a group of check boxes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: "checked" property of a html:checkbox field ... ?