【问题标题】:Thymeleaf Binding list of objectsThymeleaf 绑定对象列表
【发布时间】:2016-02-17 12:41:22
【问题描述】:

这是我从数据库中检索到的对象:

@RequestMapping("/category/edit/{id}")
@org.springframework.transaction.annotation.Transactional
public ModelAndView displayCategoryList(@PathVariable("id")Integer id){
    ModelAndView mav = new ModelAndView("category-form");
    List<CatFeatGroup> catFeatGroupList = catFeatGroupService.findGroupsForCategory(id);
    mav.addObject("catFeatGroupList",catFeatGroupList);
    return  mav;
}

这是我的表格。

<form class="form-horizontal">
    <div th:each="catFeatGroup,status : ${catFeatGroupList}" class="form-group">
        <label>Position</label><input th:field="catFeatGroupList[${status.index}].position"  th:value="${catFeatGroup.position}" />
        <label>Name</label> <input name="catGroupList[${status.index}].name" th:value="${catFeatGroup.name}" />
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

我需要使用th:field 绑定该对象,但是出现了这个错误:

无法解析为表达式:“catFeatGroupList[${status.index}].position”

【问题讨论】:

    标签: spring forms spring-mvc thymeleaf


    【解决方案1】:

    像这样添加“__”符号

    <form class="form-horizontal">
        <div th:each="catFeatGroup,status : ${catFeatGroupList}" class="form-group">
            <label>Position</label><input th:field="*{catFeatGroupList[__${status.index}__].position}"  th:value="${catFeatGroup.position}" />
            <label>Name</label> <input data-th-name="*{catFeatGroupList[__${status.index}__].name}" th:value="${catFeatGroup.name}" />
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
    </form>
    

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 2016-01-30
    • 2014-01-15
    相关资源
    最近更新 更多