I have a list of custom objects that I use as the commandName
object in my form, and usually each object contains a String label, String type (INTEGER or BOOLEAN), and a value. For the INTEGER types, the form displays the <form:input
and binds just fine. However, with BOOLEAN types, I'd like to be able to use the values strings 开发者_StackOverflow社区of 'true' and 'false' and bind them to a checkbox. This would be easy is they were Boolean types, but they are Strings, and the normal <form:checkbox
code, isn't working. I tried using hidden too, but it isn't binding correctly. Is there an easy way to do this? When I submit the form again, the checkbox values need to be a string again, i.e. (checked="true", unchecked="false").
Checkboxes don't really have multiple values - either it has the value, indicating it was checked, or it doesn't. In your case, I would bind the value you want to pass back and forth to a hidden form field, and use client-side JavaScript to set the value of the hidden form field based on whether the user clicks the checkbox or not. Additionally, on page load, Spring will set the value of the bound hidden field, and at that point you can use JavaScript to check the checkbox, or not, based on the bound value in the hidden field.
Spring Form:
<form:hidden path="mypassedfield" />
jQuery:
if($(".mycheckbox").prop("checked", true)){
$(".mypassedfield").val($(".mycheckbox").val());
}
精彩评论