【问题标题】:Spring MVC Boolean not binding to checkboxSpring MVC 布尔值未绑定到复选框
【发布时间】:2013-07-26 02:47:08
【问题描述】:

为了后人的缘故,问我自己的问题,然后回答我自己的问题。我在我的页面上使用了这样的标签:

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

此复选框将根据从数据库中提取的布尔值正确选中(或取消选中)。但是,对复选框的更改从未绑定到实体并保留回数据库。事实证明,问题出在我的实体是如何构建的。我的实体有这样的属性和方法:

private Boolean Active;

public Boolean getActive() { 
    return this.Active;
}

public Boolean setActive(Boolean active) {
    this.Active = active;
}

public boolean isActive() {
    if (getActive() == null) {
        return false;
    }
    return getActive().booleanValue();
}

事实证明,“isActive()”方法会干扰复选框上选择的值被绑定回实体上的 Active Boolean 属性。将方法重命名为其他任何名称(例如 "isPersonActive()" )可以解决问题。

【问题讨论】:

  • 在 Java 中不要使用以大写字母开头的变量或属性名!!

标签: spring model-view-controller binding checkbox boolean


【解决方案1】:

尝试在你的对象和jsp中将你的属性名称改为active(小写字母)。

private Boolean active;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2018-11-08
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多