【发布时间】:2021-11-27 03:46:31
【问题描述】:
如何从控制器中获取1-20个复选框的值?
<div>
<ul>
<li th:each="poi : ${poiList}">
<label th:for="${poi.modelName}" th:text="${poi.modelName}">modelName</label>
<input type="checkbox" th:id="${poi.modelName}"
th:name="${poi.modelName}" th:checked="false"/>
</li>
</ul>
</div>
如上,form标签中有20个复选框。
当按下发送按钮时,只有 poi1~poi20 中选中的 poi 被发送为poi6=on&poi15=on。
这个时候应该如何从控制器中获取值呢?
@PostMapping("/add")
public String save(
@RequestParam String poi1,
@RequestParam String poi2
...){
// modelName=%EB%8F%84%EB%A9%B41&poi6=on&poi15=on
if( poi == null)
...
}
不知道有没有比这种方式更好的方法。
【问题讨论】:
-
HTML 标准从不通过 POST 发送未经检查的输入。这就是为什么 Thymeleaf 添加带有下划线的隐藏字段,所以 Spring 知道如何处理它们。使用
List<Boolean> poi;接收字段列表,它可以双向工作。
标签: java spring spring-boot model-view-controller thymeleaf