• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" )
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether it is checked or not is determined by the associated property.
 
Wally Schnok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:-) 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check out the html multibox tag. That's come in handy many times for a group of check boxes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic