【问题标题】:Thymeleaf exception Neither BindingResult nor plain target object for bean nameThymeleaf 异常既不是 BindingResult 也不是 bean 名称的普通目标对象
【发布时间】:2016-11-30 14:10:25
【问题描述】:

使用@Crafalo 建议编辑

<form action="#" th:action="@{/admin/dict/adder}" th:object="${dictionary}" method="post">
    <table>
        <tr>
            <td>Typ slownika:</td>
            <td>
                <select class="form-control" th:field="*{dict}"  id="dropType">
                    <option value="0" th:text="select operator" ></option>
                    <option th:each="dict : ${dictList}" th:value="${dict.id}"
                            th:text="${dict.description}">Cos</option>
                </select>
            </td>
        </tr>

控制器

  List<Dictionary> dictList = dictService.findAllDictionaries();
        model.addAttribute("dictionary", new Dictionary());
        model.addAttribute("dictList", dictList);



Neither BindingResult nor plain target object for bean name 'dict' available as request attribute

在选择/选项中,我希望有一个这种形式的 parnet 字典,我需要创建一个子字典

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

    我猜你的问题出在你的领域

    <select class="form-control" th:field="${dictionary.description}"  id="dropType">
    

    应该是

    <select class="form-control" th:field="*{id}"  id="dropType">
    

    并且在某些地方有表格

    <form th:object="${dictionary}">
    

    更新:

    您还需要将模型中的“字典”对象发送到视图

    model.addAttribute("dictionary", new Dictionary());   
    

    另一个错误是在您的第一个选项标签中

    th:text="select operator"
    

    当你使用 th: 前缀是因为你想从控制器中读取一些值,但是如果在这种情况下你只是想写一些固定的文本,使用一个普通的选项标签,把它改成:

    <option value="0">select operator</option>                     
    

    【讨论】:

    • 我编辑了我的主要帖子。我有
      ,因为所有表单都是 post 方法,但我需要一个由模型属性创建的表单元素
    • @jan345 确保您在模型中发送对象“字典”,并从选择字段中删除前缀“字典”。检查我的回复
    • 我在 select /option 中将名称从字典更改为字典,添加了 model.addAttribute("dictionary", new Dictionary()); ,但我现在既没有 BindingResult 也没有 bean 名称 'dict' 的普通目标对象可用作请求属性
    • 不,你很困惑。首先将属性“字典”添加到模型中,在表单标签中添加 th:object="${dictionary}",在您的选择中仅保留此 th:field="*{description}" 或字典的 id,我认为应该是一些 id 而不是字符串
    • Uppss,我很困惑你能告诉我更多关于这个编辑过的代码吗?
    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 2013-05-23
    • 2020-02-21
    • 2020-08-27
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多