百里香如何传递许多物体?

问题描述 投票:0回答:1

如何在一个表单中传递多个对象?因为那不工作....请帮帮我

   <form class="form-control" action="#" th:action="@{'/'}"  method="post">


<select>
    <option th:each="city :${cities}" th:value="${city.getId()}" th:text="${city.getName()}"/>
</select>




<select>

    <option th:each="category :${categories}" th:value="${category.getId()}" th:text="${category.getDescirption()}">
    </option>

</select>
  //this line isnt correct:
    <a class="btn btn btn-primary btn-sm" href="#" 
       th:href="@{'/todo/done?id=' + ${city.id} + '&name=' + ${category.name}}">done</a>

</form>
spring thymeleaf
1个回答
0
投票

您将每个对象添加为命名模型属性。例

 @RequestMapping(value = "message", method = RequestMethod.GET)
    public String messages(Model model) {
        model.addAttribute("cities", citiesRepository.getAll());
        model.addAttribute("categories", citiesRepository.getAll());
        return "message/list";
    }

我个人认为将单个对象作为模型属性更具可读性。该对象具有每个实际属性的字段。

有关更多详细信息,请参阅官方文档https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

在您的示例中,您没有正确使用th:each。你应该将th:each作为select级别(不是在option级别)然后引用嵌套的select元素中的单个项目。

喜欢

<select th:each="city :${cities}">
    <option  th:value="${city.getId()}" th:text="${city.getName()}"/>
</select>

在您的上一个示例中,您引用了$ {city.id}和$ {category.name},但未定义城市和类别(可能是thymleaf错误消息告诉您)。

你究竟想要实现什么目标?要与选定的城市和类别建立链接吗?如果是这样,您需要单独的模型属性,如selectedCity和selectedCatetory。

© www.soinside.com 2019 - 2024. All rights reserved.