无法将复选框值注入spring bean

时间:2017-06-21 11:24:27

标签: java spring jsp spring-mvc handlebars.js

我正在使用SpringMVC并从表单中读取params。我的bean看起来像这样:

public class ChannelBean {

private Integer id;
private Integer siteId;
private String name;
private Boolean active;
private Boolean premium;


public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public Boolean getActive() {
    return active;
}
public void setActive(Boolean active) {
    this.active = active;
}
public Boolean getPremium() {
    return premium;
}
public void setPremium(Boolean premium) {
    this.premium = premium;
}
public Integer getSiteId() {
    return siteId;
}
public void setSiteId(Integer siteId) {
    this.siteId = siteId;
}

}

在前端我有把手模板:

<script id="editFormTemplate" type="text/x-handlebars-template">
    <form id="channelForm" class="form-horizontal" action="/admin/channel/save.action" method="POST">
        <input type="hidden" readonly="readonly" name="siteId" value="${selectedSiteId}">
        <input type="hidden" readonly="readonly" name="id" value="{{id}}">
        <div class="form-group">
            <label class="col-sm-2 control-label channelName">Name<span class="required">*</span></label>
                <div class="col-sm-10">
                    <input class="form-control" type="text" name="name" value="{{name}}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Premium</label>
                <div class="col-sm-10">
                    <input type="checkbox" name="premium" {{#if premium}}checked{{/if}} value="{{premium}}">
                </div>
            </div>  
        <cms:csrfToken />
        <div class="modal-footer">
            <button type="submit" class="btn btn-edit btn-primary has-spinner submitEditButton">Ok<span class="spinner"><i class="fa fa-refresh fa-spin"></i></span></button>
            <button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
        </div>
    </form>
</script>

在我收到Bean的Java控制器中,&#39; name&#39;值是否设置正确,但来自复选框的溢价是否为空?

1 个答案:

答案 0 :(得分:0)

由于您使用的是Boolean,因此您需要在页面加载时将值初始化为false。因此,要么在页面加载上初始化premiumfalse,要么

ChannelBean.java 中的private Boolean premium更改为private boolean premium