java.lang.IllegalStateException:BindingResult和bean名称的普通目标对象都不能作为请求属性使用

时间:2015-11-16 12:56:43

标签: java forms spring-boot thymeleaf

这是我的表格。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="header :: head"></head>
<body>
<div th:replace="header :: head-nav"></div>
<div id="wrapper">
    <div class="container-fluid">
            <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="${catFeatGroupList[__${status.index}__].name}" th:value="${catFeatGroup.name}" />
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
    </div>
</div>
<div th:replace="footer :: foot"></div>
</body>
</html>

这里是给它值的控制器

    @RequestMapping("/category/edit/{id}")
    @ModelAttribute("catFeatGroupList")
    @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;
    }
}

但页面返回错误

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'catFeatGroupList[0]' available as request attribute

我缺少什么

1 个答案:

答案 0 :(得分:0)

表达式th:field="${catFeatGroupList[__${status.index}__].position}"正在创建错误。

th:field属性只能在html form标记中使用,才能访问已定义表单的字段。 th:field属性生成3个html属性,idnamevalue

因此您的代码可以修改为:

<div th:each="catFeatGroup,status : ${catFeatGroupList}" class="form-group">
    <label>Position</label>
    <input th:id="${'catFeatGroupList'+__${status.index}__+'.position'}" th:name="${'catFeatGroupList['+__${status.index}__+'].position'}" th:value="${catFeatGroup.position}" />
    <label>Name</label>
    <input th:id="${'catFeatGroupList'+__${status.index}__+'.name'}"     th:name="${'catFeatGroupList['+__${status.index}__+'].name'}" th:value="${catFeatGroup.name}" />
</div>

我使用th:idth:nameth:value代替th:field

如果可以使用html form标记,则可以使用th:field,而不是使用th:idth:nameth:value这样

<form th:object="${modelObject}">
    <div th:each="catFeatGroup,status : *{catFeatGroupList}" class="form-group">
        <label>Position</label>
        <input th:field="*{catFeatGroupList[__${status.index}__].position}" />
        <label>Name</label>
        <input th:field="*{catFeatGroupList[__${status.index}__].name}" />
    </div>
</form>