Thymeleaf - 在输入标签中添加 <br>

3
我正在尝试在表单的每个输入行后添加<br>,但是Thymeleaf一直给我解析错误。
这是我遇到问题的代码片段:
<form th:if="${not #lists.isEmpty(brands)}">
<input th:each="brand : ${brands}" type="checkbox" th:value="${brand.name}" th:utext="${brand.name + <br>}" />
</form>

如果我在输入标签外添加<br>标签,它不会将其添加到每一行中。
提前感谢。

<br> 应该放在引号里吗?我不了解thymeleaf,但是对我来说,<br> 不在 " " 之间似乎很奇怪。 - Piotrek
你是说 th:utext="${brand.name}" + "<br>" 吗?如果是,那么这是 Thymeleaf 的语法错误。 - Jail
我指的是th:utext="${brand.name + '<br>'}"。 - Piotrek
那也不行。同样的解析错误。 - Jail
1个回答

5
我觉得你可能走错了方向。 th:utext会将其插入<input>节点内部。但是,根据HTML5规范<input>标签中不包含任何内容("Content model: Empty.")。
我认为你想要的更像这样:
<form th:if="${not #lists.isEmpty(brands)}">
    <th:block th:each="brand : ${brands}">
        <label th:for="${#ids.next('brand')}" th:text="${brand.name}">Brand A</label>
        <input type="checkbox" th:id="${#ids.seq('brand')}"
            name="brand" th:value="${brand.name}"/>
        <br/>
    </th:block>
</form>

如果您正在使用Spring MVC,您可能会发现这个例子很有帮助:http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#checkbox-fields


这段代码在 ${#ids.next('brand')} 处给我报错,但除此之外它修复了 br 问题。感谢提供示例的链接。 - Jail
哎呀,我觉得我漏了一个 } ... 试试修改后的版本。 - pioto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接