【问题标题】:Form handling using Thymeleaf使用 Thymeleaf 处理表单
【发布时间】:2020-01-30 06:42:52
【问题描述】:

我有以下代码/类“ServiceRequest”,我想用它在 Spring Rest 和 Thymeleaf 中编写一个 POST 表单和 Rest 端点。

public class ServiceRequest {

 private Map<String, ServiceObject> serviceMap = new HashMap<>();
}

public class ServiceObject {
  private String id;
  private String name;
}

我完成了 java 部分,但对将 Maps 作为表单对象处理一无所知。如果有人在这里分享任何示例或编写一些示例 Thymeleaf 脚本,我将不胜感激。

【问题讨论】:

    标签: java spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    你可以这样做:

    <tr th:each="serviceItem: ${ServiceRequest.serviceMap}">
        <td th:text="${serviceItem.key}">keyvalue</td>
        <td th:text="${serviceItem.value}">data</td>
    </tr>
    

    使用 th:each 来迭代地图,你会得到java.util.Map.Entry 类型的项目变量。 参考Doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 2017-04-26
      • 2017-07-15
      • 2017-05-02
      • 1970-01-01
      相关资源
      最近更新 更多