Search code examples
springspring-mvcthymeleaf

Thymeleaf exception Neither BindingResult nor plain target object for bean name


Edited with @Crafalo suggestion

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

Controller

  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

In select/option i want have a parnet Dictionary in this form i need create a child Dictionary


Solution

  • I guess your problem is in your th:field

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

    Should be

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

    And in some place have the form

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

    UPDATE:

    You also need to send to the view the "dictionary" object in the model

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

    Another mistake is in your first option tag

    th:text="select operator"
    

    When you use the th: prefix is because you want to read some value from the controller, but if in this case you just want to write some fixed text, use a normal option tag, change it for:

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