【问题标题】:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean nameavailable as request attributejava.lang.IllegalStateException:Bean 名称的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2016-02-17 14:02:33
【问题描述】:

这是我的表格。

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

我错过了什么

【问题讨论】:

    标签: java forms spring-boot thymeleaf


    【解决方案1】:

    表达式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>
    

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2018-01-07
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多