Thymeleaf对象的绑定列表 [英] Thymeleaf Binding list of objects

查看:1117
本文介绍了Thymeleaf对象的绑定列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的表单。

 < form class =form-horizo​​ntal> 
< div th:each =catFeatGroup,status:$ {catFeatGroupList}class =form-group>
< label>位置< / label>< input th:field =catFeatGroupList [$ {status.index}]。positionth:value =$ {catFeatGroup.position}/>
< label>名称< / label> < input name =catGroupList [$ {status.index}]。nameth:value =$ {catFeatGroup.name}/>
< / div>
< button type =submitclass =btn btn-default>提交< / button>
< / form>

我需要使用 th:field 来绑定该对象,但这个错误显示:


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



解决方案

添加这样的__符号

 < form class =form-horizo​​ntal> 
< div th:each =catFeatGroup,status:$ {catFeatGroupList}class =form-group>
< label> Position< / label>< input th:field =* {catFeatGroupList [__ $ {status.index} __]。position}th:value =$ {catFeatGroup.position} />
< label>名称< / label> < input data-th-name =* {catFeatGroupList [__ $ {status.index} __]。name}th:value =$ {catFeatGroup.name}/>
< / div>
< button type =submitclass =btn btn-default>提交< / button>
< / form>


Here's my Object that I've retrieved from the DB:

@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;
}

Here's my form.

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

I need to use th:field to bind that object however this error shows up:

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

解决方案

Add the "__" notation like this

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

这篇关于Thymeleaf对象的绑定列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆