简体   繁体   中英

Thymeleaf checkbox can't default to checked when using th:field?

Does anyone know how to add a default setting of checked in a Thymeleaf, SpringBoot, Java project, to an input field for a Single checkbox? I have to use th:field because I am later submitting this form via email and if I use th:name with the checked box default, the default works but email doesn't.

Any suggestions?

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

Email html code to pull value on email:

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

I have this variable set as private Boolean directBox in the model.

I've tried everything: th:checked=checked , value=true , value=checked , set the value and checked ="${directBox}" none of this works.

Is there a solution out there?

Figured out a way to use th:field and have it default to checked. Have to set it in my controller, like Gustavo mentioned. Code example below.

...
directBind.setDirectBox(true);
directBind.setServiceLevel(true);
model.addAttribute("directBind", directBind);
return "directBind";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM