PDA

View Full Version : How to use checkboxes with Spring MVC



j2ux
Aug 30th, 2004, 03:49 AM
How do I have to handle checkboxes with Spring/JSP ?

The request always returns null, no matter if the box is checked or not. The corresponding field type in the domain object is of type boolean with getter and setter.

I've had a look at the jPetstore sample but this didn't help either.

Thanks,
Lars

fmourioux
Aug 30th, 2004, 03:58 AM
Hi,

U can to a look at :

http://forum.springframework.org/showthread.php?t=9686&highlight=checkbox

Hope it'll help u ...

Fabien.

j2ux
Aug 30th, 2004, 07:54 AM
I found the solution myself, there is manual handling involved (btw this could be handled by the framework...)

You have to handle this in "onBind" or "onBindAndValidate":

if (request.getParameter("active") == null) {
User user = (User) command;
user.setActive(false);
}

Just for the record ...

Regards,
Lars

uze
Aug 30th, 2004, 10:33 AM
Have you put a value parameter in your <input> tag? If not, it will always return null when evaluated. To make automatic boolean binding, declare your tag like this:

<input type="checkbox" name='myName" value="true" />

Cheers,

Uze

lis
Aug 31st, 2004, 02:17 AM
by the way, when form field is Boolean type I do form handling by the following way:

some.jsp


<spring&#58;bind path="myForm.booleanFieldName">
<input type="checkbox" name="<c&#58;out value="$&#123;status.expression&#125;"/>" value="true" <c&#58;if test="$&#123;status.value&#125;">checked</c&#58;if>>
</spring&#58;bind>


SomeController.java


protected void onBind&#40;HttpServletRequest request, Object command&#41;... &#123;
MyForm myForm = &#40;MyForm&#41; command;
myForm.setBooleanFieldName&#40;
Boolean.valueOf&#40;RequestUtils.getBooleanParameter&#40;r equest, "booleanFieldName", false&#41;&#41;&#41;;
....


for your case you can do:


protected void onBind&#40;HttpServletRequest request, Object command&#41;... &#123;
MyForm myForm = &#40;MyForm&#41; command;
myForm.setBooleanFieldName&#40;RequestUtils.getBoolean Parameter&#40;request, "booleanFieldName", false&#41;;

uze
Aug 31st, 2004, 07:34 PM
for your case you can do:


protected void onBind&#40;HttpServletRequest request, Object command&#41;... &#123;
MyForm myForm = &#40;MyForm&#41; command;
myForm.setBooleanFieldName&#40;RequestUtils.getBoolean Parameter&#40;request, "booleanFieldName", false&#41;;


If you just want simple binding, this is redundant because springs has a default boolean propertyEditor that correctly convert "true" or "false" strings to boolean type on form submit. Check it out. Don't add any more code in you controller and check for the update in its onSubmit() event.

Uze

j2ux
Sep 1st, 2004, 02:44 AM
If you just want simple binding, this is redundant because springs has a default boolean propertyEditor that correctly convert "true" or "false" strings to boolean type on form submit. Check it out. Don't add any more code in you controller and check for the update in its onSubmit() event.

Yes, normally I'd think Spring should handle this like other properties (String, etc). But this didn't work in my case.

Thanks,
Lars

lis
Sep 1st, 2004, 04:23 AM
If you just want simple binding, this is redundant because springs has a default boolean propertyEditor that correctly convert "true" or "false" strings to boolean type on form submit. Check it out. Don't add any more code in you controller and check for the update in its onSubmit() event.

Uze
Yes, spring has a default boolean property editor that correctly convert "true" or "false" strings to boolean type on form submit.
But, it is work only in case, when your boolean field has "false" initial value, not "true".
For the following usecases it is not work correctly:
1) form backing object has boolean field with "true" initial value and you bind this field with checkbox (it is frequently occurring for edit persistent objects for example), user unmark chackbox and submit form, but ours form backing object boolean field still has "true" value;
2) it is particular case of 1) usecase; field has "false" initial value, user mark this checkbox as checked but form has been not validated and HTML form redisplayed for user with error messages, user change one's mind and unmakr checkbox and submit form again; unfortunately ours form backing object boolean field still has "true" value.

The reason is nature of checkboxes. When checkbox is not marked, checkbox name has not present on request parameters.

The solution of this problem is handling chackboxes manually in onBind method, or in other convenient place. You can handling this by calling RequestUtils.getBooleanParameter method or simply by check request parameter as suggest j2ux.

oliverhutchison
Sep 1st, 2004, 06:11 AM
Spring actualy support this. Though he only only documentation I could find is here :

http://www.springframework.org/docs/api/org/springframework/web/bind/ServletRequestDataBinder.html#setFieldMarkerPrefix (java.lang.String)

Basicly what you should do is this:



<spring&#58;bind path="myForm.booleanFieldName">
<input type="hidden" name="_<c&#58;out value="$&#123;status.expression&#125;"/>" value="visible" />
<input type="checkbox" name="<c&#58;out value="$&#123;status.expression&#125;"/>" value="true" <c&#58;if test="$&#123;status.value&#125;">checked</c&#58;if>>
</spring&#58;bind>


Note the hidden field with same name as the checkbox except for the leading underscore.

Ollie

lis
Sep 1st, 2004, 07:10 AM
Spring actualy support this. Though he only only documentation I could find is here :

http://www.springframework.org/docs/api/org/springframework/web/bind/ServletRequestDataBinder.html#setFieldMarkerPrefix (java.lang.String)


Thanks oliverhutchison for link! Very convenient solution :)
I did not know this feature till now. I see it has appeared since 1.1-rc1 version.

uze
Sep 1st, 2004, 09:47 AM
Lis,

You are indeed right, without the underscore trick, it does not work correctly with checkboxes!

Uze

rafeco
Sep 22nd, 2004, 01:28 PM
When I try to use the technique mentioned here, as follows:


<input type="hidden" name="_<c&#58;out value="$&#123;status.expression&#125;"/>" value="visible" />
<input type="checkbox" name="<c&#58;out value="$&#123;status.expression&#125;" />" value="true" <c&#58;if test="$&#123;status.value&#125;">checked</c&#58;if> />

If I submit the form with the checkbox checked, get a validation error, and submit the form again with the checkbox unchecked, when the form is returned, the checkbox is still checked. If I submit the form with the checkbox unchecked the first time, it shows up as unchecked as you'd hope.

Is there a different expression that should be used in the test attribute of the c:if tag to get this to work properly?

liying
Oct 25th, 2007, 03:42 PM
What if the checkbox value is not boolean, and it has multiple values? For example,



<input type="checkbox" name="categories" value="Assignment" />
<input type="checkbox" name="categories" value="Online Tutorial" />
...


Now I did it in a painful way. It works but I don't like it. Is there a better way to do that?


<spring:bind path="categories">
<input type="checkbox" name="categories" value="Assignment"<c:forEach items="${status.value}" var="s"><c:if test="${s eq 'Assignment'}"> checked</c:if></c:forEach> />
<input type="checkbox" name="categories" value="Online Tutorial"<c:forEach items="${status.value}" var="s"><c:if test="${s eq 'Online Tutorial'}"> checked</c:if></c:forEach> />
.....



Thanks,
Liying

ggt
May 4th, 2009, 04:21 PM
I create the following velocity macro to show booleans as checkboxes with Spring MVC + Velocity:

#macro(springFormBoolean $path $attributes)

#springBind($path)

<input type="hidden" name="_${status.expression}" value="visible" />

<input type="checkbox" name="${status.expression}" value="true"

#if(${status.value} == "true")


checked

#end

${attributes} #springCloseTag()
#end

Edit usage:


#springFormBoolean('myObject.myBooleanField' '')

Read only usage:


#springFormBoolean('myObject.myBooleanField' 'disabled')