首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thymeleaf + Spring复选框不绑定

Thymeleaf + Spring复选框不绑定
EN

Stack Overflow用户
提问于 2014-12-30 18:56:07
回答 2查看 4.4K关注 0票数 0

下面是我的代码片段:

车辆类别:

代码语言:javascript
复制
// 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;
}

蒂梅莱夫:

代码语言:javascript
复制
                        <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:

代码语言:javascript
复制
<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>

问题:

布尔值永远不会在submit上设置。我试过另一种方法。使用List对象并将该变量绑定到复选框,并为每个复选框使用适当的th:value="'SOMETEXT'“。但是这个对象也是空的。

正如您所看到的,我预先将一个值设置为true,以查看它是否会出现在html端。成功了。因此,它至少检索了值,但如果它改变了,它就不会设置它。

另外,正如您所看到的,我尝试使用“th:value=”复选框“查看这是否有帮助。但事实并非如此。

Thymeleaf版本2.1.4

春季4.1.4

弹簧WebFlow 2.4.0

任何帮助或建议都会很好。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-02 18:46:24

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

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

我所拥有的:

代码语言:javascript
复制
<view-state id="duplicate" view="duplicates">
    <transition on="continue" to="checkVehicleInfo" />
    <transition on="back" to="finished" />
</view-state>

视图状态中缺少的属性是:

代码语言:javascript
复制
model="customerModel"

在使用Spring时,表单提交需要这样做

票数 0
EN

Stack Overflow用户

发布于 2014-12-30 19:25:34

我想可能的解决办法是:

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

代码语言:javascript
复制
<label>Registration Card
<input type="checkbox" name="retCardChecked"/>

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

代码语言:javascript
复制
@RequestParam("retCardChecked") boolean retCardChecked, ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27710969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档