Checkbox values do not bind into object when false?

给你一囗甜甜゛ 提交于 2019-11-28 20:42:20
Ralph

Spring has a built in workaround.

Simply add this additional hidden field to the form:

<input type="hidden" value="on" name="_active"/>

The parameter with a leading underscore is some kind of marker, to indicate the existence of a checkbox parameter with the same name, but without the underscore.

Spring should now set lesson.active to false if only _active=on is submitted.

I think that you should use springframework tags - why are you using plain html? If your view is JSP page just import:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

and then you can use:

<form:checkbox path="path" label="label" />

which handles the problem.

You can use spring framework tags like it will automatically set you selected value to bean class and then it will be very easy to get its value in your controller class.

on path you need to put your bean variable name

and to use this you need to give command name to your form and use that command name to bind your bean object to that jsp

model.addAttribute("command name" , bean object) use this in your controller to bind object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!