如何从 servlet 动态设置/获取复选框的值
这更多的是一个逻辑问题。我在网页上有一个复选框,并将 Servlet 请求参数中的值存储在布尔对象 (Java) 中。逻辑是,如果复选框的请求参数不为空,则使该对象为 true,否则为 null。当再次调用该页面时,如果其存储的值为 true,则将复选框标记为“已选中”。
this.checkbox = (servlet.getParameter("checkbox")!=null && servlet.getParameter("checkbox").contentEquals("on"))?true:null;
当我保留该复选框对象时,问题就开始了。我首先使用持久数据填充页面,然后使用 servlet 值填充该页面。如果复选框值在数据库中存储为 true,并且用户在页面上取消选中它并提交它,则由于该复选框的 servlet 参数变为空,我无法将该复选框设置为空。因此,复选框始终显示持久值,因为它永远不会被覆盖。那么有人可以建议我对填充对象值的方式进行一些逻辑更改吗?
This is more of a logic question. I have a checkbox on a webpage and I store its value from the servlet request parameters in a Boolean object (Java). The logic is, if the request parameter for the checkbox is not null, make the object true, otherwise it's null. When the page is called again, it marks the checkbox as "checked" if its stored value is true.
this.checkbox = (servlet.getParameter("checkbox")!=null && servlet.getParameter("checkbox").contentEquals("on"))?true:null;
The problem starts when I persist that checkbox object. I first populate the page with the persisted data and then fill it with the servlet values. If the checkbox value is stored as true in the database, and the user unchecks it on the page and submits it, since the servlet parameter for the checkbox becomes null, I am unable to make the checkbox null. So the checkbox always show the persisted value since its never gets overwritten. So can anyone suggest some logic change in how I am filling the object value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
复选框的唯一有效测试是查看 getParameter() 是否返回 null 。
如果返回值为
null
,则表示未选中,否则表示已选中。请注意,如果您希望在首页演示文稿上选中该复选框,则应存在
checked
属性;如果您希望取消选中该复选框,则不应存在该属性。应始终检查
参数值,如下例所示。对于此示例,您的 servlet 处理代码中可能包含以下逻辑:
注意,如果用户在上面的示例中选中了复选框,
getParameter
将返回"cb_id_value"
,但因为您通常有一个带有专用名称的复选框,所以您不必检查该值。顺便说一句,我在您的示例中注意到您正在使用
servlet
来getParameter
。我希望在您的系统中它是HTTPServletRequest
的绰号。The only valid test for checkbox is to see whether
getParameter()
returnsnull
or not.If the return value is
null
it's unchecked, otherwise it's checked.Note, that
checked
attribute should be present if you want the checkbox be checked on the first page presentation and should not be present at all if you want it unchecked. The parameter value should always bechecked
, as in the example below.For this example, you may have this logic in your servlet processing code:
Note, that if the checkbox is checked by the user in the above example
getParameter
will return"cb_id_value"
, but because you usually have a single checkbox with a dedicated name you don't have to check the value.BTW, I've noticed in your example that you are using
servlet
togetParameter
. I hope that in your system it is a moniker forHTTPServletRequest
.听起来您可能没有保留复选框字段的更改值,或者您在处理空值时遇到一些混乱,导致该字段始终显示为已选中。
通过将字段设置为不允许空值并具有适当的默认值,尝试确保数据库字段始终包含有效的布尔值。
提交表单后,如果选中该复选框,您应该将数据库值更新为
true
,否则应将数据库值更新为false
。在显示表单时,仅当数据库值为
true
时,才应在标记中包含“checked”属性。It sounds like you might not be persisting a changed value of the checkbox field, or you've got some confusion in the handling of null values causing the field to always show as checked.
Try to make sure your database field always contains a valid boolean, by setting the field to not allow nulls and to have an appropriate default value.
On submission of the form, you should be updating the database value to
true
if the checkbox is checked andfalse
otherwise.On showing the form you should include the "checked" attribute to the tag only if the database value is
true
.