Thymeleaf + Spring Checkbox不具约束力

时间:2014-12-30 18:56:07

标签: spring checkbox thymeleaf

这是我的代码片段:

车辆类:

// 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;
}

Thymeleaf:

                        <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 =”复选框“”查看是否有帮助。它没有。

Thymeleaf Version 2.1.4

Spring 4.1.4

Spring WebFlow 2.4.0

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

由于

2 个答案:

答案 0 :(得分:0)

我猜可能的解决方案是:

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

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

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

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

答案 1 :(得分:0)

SIGH ......我找到了解决方案。

我很容易忽略了我所在的子流和视图状态的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

时,表单提交需要这样做
相关问题