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" )
<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 )
:-) 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" !?
Joined: May 05, 2000
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 ]
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.
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.
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 ]
Joined: May 31, 2001
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
Joined: May 05, 2000
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 ]