【问题标题】:How to get Item-Quantity map using Spring MVC forms?如何使用 Spring MVC 表单获取项目数量图?
【发布时间】:2022-01-22 13:31:07
【问题描述】:

对于每道菜,我都有一个输入来指定菜的数量,但是如何将其转换为 DTO 的 DishQuantityMap?其中 firstInteger - discId,second - 订购的菜肴数量。

<form action="#" th:action="@{/greeting}" th:object="${Order}" method="post">
<span th:each="dish : ${dishList}">
<input id="number" type="number" value="42">
</span>
</form>

我知道我的代码中缺少 th:field,那是因为我不知道如何正确实现它。

public class Order {
    private String userName;
    Map<Integer, Integer> DishQuantityMap;
}

【问题讨论】:

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


    【解决方案1】:

    我不知道dishList 是什么,但如果它有一个id 字段,那么您的表单代码将类似于:

    <form action="#" th:action="@{/greeting}" th:object="${Order}" method="post">
      <span th:each="dish : ${dishList}">
        <input th:field="*{dishQuantityMap[__${dish.id}__]}" type="number" />
      </span>
    </form>
    

    【讨论】:

    • 我知道写谢谢是违反规则的,但你甚至无法理解我对你上面写的代码的重视程度,我尝试这样做了 2 天,没有人知道如何工作与 Thymeleaf 中的地图。谢谢!
    • 你能看一下吗,不知何故,我的JS在添加此行后停止工作stackoverflow.com/questions/70817904/…
    猜你喜欢
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2016-06-04
    • 1970-01-01
    • 2014-08-24
    • 2013-04-05
    相关资源
    最近更新 更多