使用th:field时,Thymeleaf复选框无法默认选中?

问题描述 投票:0回答:1

有谁知道如何在Thymeleaf,SpringBoot,Java项目中将checked的默认设置添加到Single复选框的输入字段中?我必须使用th:field,因为我稍后通过电子邮件提交此表单,如果我使用带有复选框默认值的th:name,则默认有效但电子邮件不能。

有什么建议?

<label>
       <input type="checkbox" id="serviceLevel"  th:field="*{serviceLevel}" th:checked="${serviceLevel}"/>
Affiliate Serviced</label>

通过电子邮件发送html代码来提取电子邮

<tr class="emailRow">
  <h3>Direct: </h3>
    <td class="bodycopy" th:text="${directBind.directBox}">
    </td>
</tr>

我将此变量设置为模型中的私有布尔值directBox。

我已经尝试了一切:th:checked=checkedvalue=truevalue=checked,设置值和checked ="${directBox}"没有这个工作。

那里有解决方案吗?

java spring-boot thymeleaf
1个回答
0
投票

想出了一种使用th:field的方法,并默认选中它。必须在我的控制器中设置它,就像Gustavo提到的那样。代码示例如下。

...
directBind.setDirectBox(true);
directBind.setServiceLevel(true);
model.addAttribute("directBind", directBind);
return "directBind";
© www.soinside.com 2019 - 2024. All rights reserved.