spring - Thymeleaf 绑定(bind)对象列表

标签 spring forms spring-mvc thymeleaf

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

@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 来绑定(bind)该对象,但是出现了这个错误:

Could not parse as expression: "catFeatGroupList[${status.index}].position"

最佳答案

像这样添加“__”符号

<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>

关于spring - Thymeleaf 绑定(bind)对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33734995/

相关文章:

java - 使用 Spring MVC 如何为 java 8 LocalDateTime 添加自定义 JsonSerializer?

spring-mvc - 使用 MvcMock 将 HttpServletRequest 设置为测试方法参数

java - Jackson数据绑定(bind)时如何获取Pojo名称引用的Json对象

spring - 使用 spring webservice 模板的原始 xml 响应

forms - 当文件包含在GSP表单中时,Grails不会更新数据对象吗?

java - 动态序列化/反序列化

spring - 如何避免在执行长时间运行计算的 Spring WebFlux Controller 中使用 Kotlin Coroutines 的 GlobalScope

java - 是否可以从 Spring 中的 scriptlet 调用 Controller 方法

javascript - 禁用提交,但如果按下后退按钮,请确保其重新启用

javascript - 单击按钮且页面更改时滚动回首页