【问题标题】:Unable to bind list variable from thymleaf view in spring mvc无法从spring mvc中的thymeleaf视图绑定列表变量
【发布时间】:2017-11-10 12:07:08
【问题描述】:

我正在将一个列表绑定到 thymleaf 视图中的字段,但在控制器中为空。考虑到它不是null

<form th:object="${obj}"
  <input type="hidden" th:field="*{someList}" th:value="${obj.getSomeList()}">

POJO 是这样的:

public class Foo {
  private int id;
  private List<Some> someList;

  //setter getter

}

如果我以与控制器相同的方式绑定id,请帮助如果我特别注意List

我的控制器:

@RequestMapping
public String bar(@ModelAttribute("obj") Foo foo)

【问题讨论】:

  • 嗨,Anil,这里在哪里循环?你需要一个 each 循环标签来获取列表项
  • @Pradeep ${obj} 不是列表,它的变量是列表。我想知道当它已经设置/可用时,我是否可以获得它的全部价值。
  • 加 1 谢谢,这很奇怪,但必须这样做。
  • 所以你可以通过创建一个 foreach 循环来完成这些事情。我应该继续创建一个答案以便你接受吗?

标签: java spring spring-mvc thymeleaf


【解决方案1】:

您好 Anil 将值设置为变量后,您需要使用每个标签遍历列表 请找到语法

<form th:object="${obj}" th:action="@{/list}" action="void(0)" method="post">
    <tr>    
        <td th:field="*{id}" th:text="${obj.id}" /> 
    </tr>
    <tr th:each="l , i : ${obj.someList}">
        <input type="hidden" th:field="*{someList[__${i.index}__].something}" />
    </tr>
    <input type="submit" class="btn btn-success" />
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 2016-05-27
    • 2022-01-15
    • 2016-12-07
    相关资源
    最近更新 更多