如何从 servlet 动态设置/获取复选框的值

发布于 2024-11-26 03:26:05 字数 490 浏览 3 评论 0原文

这更多的是一个逻辑问题。我在网页上有一个复选框,并将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-12-03 03:26:05

复选框的唯一有效测试是查看 getParameter() 是否返回 null 。

如果返回值为null,则表示未选中,否则表示已选中。

请注意,如果您希望在首页演示文稿上选中该复选框,则应存在 checked 属性;如果您希望取消选中该复选框,则不应存在该属性。应始终检查参数值,如下例所示。

<label>
  <input
    type="checkbox" 
    id="cb_id"
    name="cb_id"
    value="cb_id_value"
    checked="checked"
  />
  My label
</label>

对于此示例,您的 servlet 处理代码中可能包含以下逻辑:

HTTPServetRequest request = ...;

boolean cbState = request.getParameter( "cb_id" ) != null;

注意,如果用户在上面的示例中选中了复选框,getParameter 将返回 "cb_id_value" ,但因为您通常有一个带有专用名称的复选框,所以您不必检查该值。

顺便说一句,我在您的示例中注意到您正在使用 servletgetParameter。我希望在您的系统中它是 HTTPServletRequest 的绰号。

The only valid test for checkbox is to see whether getParameter() returns null 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 be checked, as in the example below.

<label>
  <input
    type="checkbox" 
    id="cb_id"
    name="cb_id"
    value="cb_id_value"
    checked="checked"
  />
  My label
</label>

For this example, you may have this logic in your servlet processing code:

HTTPServetRequest request = ...;

boolean cbState = request.getParameter( "cb_id" ) != null;

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 to getParameter. I hope that in your system it is a moniker for HTTPServletRequest.

并安 2024-12-03 03:26:05

听起来您可能没有保留复选框字段的更改值,或者您在处理空值时遇到一些混乱,导致该字段始终显示为已选中。

通过将字段设置为不允许空值并具有适当的默认值,尝试确保数据库字段始终包含有效的布尔值。

提交表单后,如果选中该复选框,您应该将数据库值更新为 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 and false otherwise.

On showing the form you should include the "checked" attribute to the tag only if the database value is true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文