Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

html:checkbox problem

 
Santosh Maskar
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have "<html:checkbox property="selectedCheckBox" value="msgId"/>" in my JSP

here are the respective getter/setter methods for this

private boolean selectedCheckBox= false;

public boolean isSelectedCheckBox() {
return selectedCheckBox;
}
/**
* @param selectedCheckBox The selectedCheckBox to set.
*/
public void setSelectedCheckBox(boolean selectedCheckBox) {
this.selectedCheckBox = selectedCheckBox;
}

When I try to get the value of checkbox(when used checked from JSP) in my action class It always return fase whether it is checked or unchecked

Can someone help on this.

-santosh
[ May 01, 2006: Message edited by: Santosh Maskar ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change

<html:checkbox property="selectedCheckBox" value="msgId"/>

to

<html:checkbox property="selectedCheckBox" value="true"/>

For a checkbox, the value attribute indicates the value that the property in the ActionForm will be populated with if the box is checked.
 
Zip Ped
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is a problem in Struts. One way I know to work around the problem is to get the checkbox value from the request parameter and then use logic in your Java code to check the value of the checkbox property being returned in the parameter and then setting the property of ur checkbox in the bean.

e.g. String checkValue = request.getParameter("[checkBoxProperty]");

if(checkValue.equals(true)){
}
 
Zip Ped
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is a problem in Struts. One way I know to work around the problem is to get the checkbox value from the request parameter and then use logic in your Java code to check the value of the checkbox property being returned in the parameter and then setting the property of ur checkbox in the bean.

e.g. String checkValue = request.getParameter("[checkBoxProperty]");

if(checkValue.equals("true")){
beanName.setCheckBoxProperty(true);
} else{
//....
}
 
Thomas vezhamb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Struts have one problem as it will send value to form bean only if the check box is selected. For overcoming this issue,in java script u need to give the value externally.

For exmp.
if(document.forms[0].preselected.checked){
document.forms[0].preselected.value="true";
}else{
document.forms[0].preselected.value="false";
}

Where as preselected is the field in form bean, which we are using with check box name also, like

<html:checkbox property="preselected" value="on"/>

It should work as you expect....

Regards,
Thomas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic