【问题标题】:Thymeleaf + Spring Checkboxes not bindingThymeleaf + Spring 复选框未绑定
【发布时间】:2014-12-30 18:56:07
【问题描述】:

这是我的sn-ps代码:

车辆类别:

// Duplicates
private Boolean regCard = true;
private Boolean decal;
private Boolean title;

public Boolean getRegCard() {
    return regCard;
}

public void setRegCard(Boolean regCard) {
    this.regCard = regCard;
}

public Boolean getDecal() {
    return decal;
}

public void setDecal(Boolean decal) {
    this.decal = decal;
}

public Boolean getTitle() {
    return title;
}

public void setTitle(Boolean title) {
    this.title = title;
}

百里香:

                        <form method="post" th:action="${flowExecutionUrl}" th:object="${customerModel.vehicle}">
                        <div class="duplicates">
                            <ul>
                                <li><label>What do you need to duplicate?</label>
                                    <div class="regTitle">
                                        <label>Registration Card 
                                            <input type="checkbox" th:field="*{regCard}"/>
                                        </label> 
                                        <label>Decal 
                                            <input type="checkbox" th:field="*{decal}"/>
                                        </label> 
                                        <div th:switch="*{vehicleType}"> 
                                            <div th:case="'BR'">
                                                <label>Title 
                                                    <input th:type="checkbox" th:field="*{title}"/>
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            </ul>
                            <div class="clear"></div>
                        </div>
                        <div class="btm-btn-row">
                            <div class="btm-btn-left">
                                <a th:href="@{'~' + ${flowExecutionUrl}(_eventId='back')}">back</a>
                            </div>
                            <div class="btm-btn-right">
                                <input type="submit" value="continue" name="_eventId_continue" />
                            </div>
                        </div>
                    </form>

生成的 HTML:

<form method="post" action="/IllinoisRVSWeb/main-flow?execution=e1s4">
                        <div class="duplicates">
                            <ul>
                                <li><label>What do you need to duplicate?</label>
                                    <div class="regTitle">
                                        <label>Registration Card 
                                            <input type="checkbox" id="vehicle.regCard1" name="vehicle.regCard" value="true" checked="checked" /><input type="hidden" name="_vehicle.regCard" value="on" />
                                        </label> 
                                        <label>Decal 
                                            <input type="checkbox" id="vehicle.decal1" name="vehicle.decal" value="true" /><input type="hidden" name="_vehicle.decal" value="on" />
                                        </label> 
                                        <div> 
                                            <div>
                                                <label>Title 
                                                    <input type="checkbox" id="vehicle.title1" name="vehicle.title" value="true" /><input type="hidden" name="_vehicle.title" value="on" />
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            </ul>
                            <div class="clear"></div>
                        </div>
                        <div class="btm-btn-row">
                            <div class="btm-btn-left">
                                <a href="/IllinoisRVSWeb/main-flow?execution=e1s4&amp;_eventId=back">back</a>
                            </div>
                            <div class="btm-btn-right">
                                <input type="submit" value="continue" name="_eventId_continue" />
                            </div>
                        </div>
                    </form>

问题:

提交时永远不会设置布尔值。 我试过另一种方式。使用 List 对象并将该变量绑定到复选框,并为每个复选框使用正确的 th:value="'SOMETEXT'" 。但该对象也保持为空。

如您所见,我事先将一个值设置为 true 以查看它是否会显示在 html 端。它是成功的。所以它至少会检索该值,但如果它改变了它就不会设置它。

另外,如您所见,我尝试使用“th:value="checkbox"” 来查看是否有帮助。它没有。

Thymeleaf 版本 2.1.4

春季 4.1.4

Spring WebFlow 2.4.0

任何帮助或提示都会很棒。

谢谢

【问题讨论】:

  • 在我正在开发的网站的另一个页面中,我能够成功使用复选框并将正确的布尔值发送到后端。我正在比较这两个页面,看看我做了什么不同,但乍一看没有发现任何差异。

标签: spring checkbox thymeleaf


【解决方案1】:

我猜可能的解决方案是:

将复选框值作为请求参数传递

<label>Registration Card
<input type="checkbox" name="retCardChecked"/>

然后在控制器中捕获它并设置为 Vehicle 对象

@RequestParam("retCardChecked") boolean retCardChecked, ...

【讨论】:

  • 我正在使用 Spring WebFlow,所以我目前没有实现任何控制器。如果我找不到合适的解决方案来解决我的问题,那么我可以为此实例使用控制器。虽然我宁愿坚持我的设计方案,如果可能的话。
  • 我去看看 WebFlow ))
【解决方案2】:

叹息...好吧,我找到了解决方案。

我很容易忽略了我所处的子流和视图状态的 WebFlow xml def 文件。

我有什么:

<view-state id="duplicate" view="duplicates">
    <transition on="continue" to="checkVehicleInfo" />
    <transition on="back" to="finished" />
</view-state>

视图状态中缺少的属性:

model="customerModel"

使用Spring WebFlow时表单提交需要这个

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多