【问题标题】:Submit Map<String,Integer> Thymeleaf提交地图<String,Integer> Thymeleaf
【发布时间】:2015-12-28 06:02:23
【问题描述】:

模型有 Map(String,Integer),此地图显示在页面上的复选框中,例如:

<ul>
  <li th:each="item : ${map}">
    <input type="checkbox" th:checked="${item.value} == 1" th:id="${item.key}"/>
    <label th:for="${item.key}" th:text="${item.key}"/>
  </li>
</ul>

我应该如何提交复选框状态更改?

【问题讨论】:

标签: java html spring-mvc servlets thymeleaf


【解决方案1】:

如果您使用 Spring MVC 作为您的应用程序并使用 Thymeleaf 作为您的视图引擎,请查看此部分关于动态表单并使用它们:

http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields

基本上,您想要做的是拥有一个对象,其中包含一个 Map 作为变量,然后当它到达控制器时,它将绑定您的所有输入。

例如

public class MyObject {
    Map<String, Integer> myMap;

// getters and setters
}

【讨论】:

  • 这似乎是有用的信息,但我不清楚如何在我的情况下使用它。
  • 您应该复习一下您的框架 MVC,以了解信息是如何流动的。了解数据如何在视图和控制器之间传递对于您理解解决方案至关重要,并且没有更简单的方法来编写/演示它。
猜你喜欢
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 2012-05-17
  • 2012-05-26
相关资源
最近更新 更多