Search code examples
javaspringthymeleaf

ThymeLeaf: Neither BindingResult nor plain target object for bean name available as request attribute


I am trying to build a simple select and Option list in thymeleaf, but i keep getting the error

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

I am pretty sure there is something wrong with my bol.html. But not able to figure out the missing mappings.

Below is my Controller:

 @Controller
    public class BillOfLadingController {

    @Autowired
    private DepotList depotList;

    @GetMapping("/BillOfLading")
    public String getBOLForm(){
        return "bol";
    }

    @PostMapping("/BillOfLading")
    public String Login(@ModelAttribute(name="BOLForm") BOLForm bolForm,Model model) {
        model.addAttribute("BOLForm", bolForm);
        List<DepotDetailEntity> depotDropDown = depotList.getDepots().getDepotDetail();
        if(depotDropDown.size() == 0) {
            return "login";
        }
        model.addAttribute("depots", depotDropDown);
        return "bol";
    }
}

The Form Class

    @Component
    public class BOLForm {
    private String BOL;
    private String depotId;
    public String getBOL() {
        return BOL;
    }
    public void setBOL(String bOL) {
        BOL = bOL;
    }
    public String getDepotId() {
        return depotId;
    }
    public void setDepotId(String depotId) {
        this.depotId = depotId;
    }
}

the bol.html

 <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="ISO-8859-1">
    <title>BillOfLading Request Page</title>
    </head>
    <body>
	<h1>BillOfLading Report</h1>
	<form th:action="@{/BillOfLading}" th:object="${BOLForm}" method="post">
			<label for="bol">BOL No.</label>
			<input type="text" id="bol" name="bol">
			<br/>
			<table>
            <tr>
                <td>Select DC:</td>
                <td>
                    <select th:field="*{depotId}">
                         <option value=""> -- </option>
                         <option th:each="depot : ${depots}"
                            th:value="${depot.depotId}"
                            th:utext="${depot.depotName}"/>
                  </select>
                </td>
            </tr>
            <tr>
                <td><input name="submit" type="submit" value="submit" /></td>
            </tr>
        </table>
	</form>
</body>
</html>

When I replace my bol.html as below, it works:

<form th:action="@{/BillOfLading}" th:object="${BOLForm}" method="post">
    <label for="bol">BOL No.</label>
    <input type="text" id="bol" name="bol">
    <br/>
    <label for="depotId">DepotID </label>
    <input type="text" id="depotId" name="depotId">
    <br/>

enter image description here


Solution

  • In the controller, you need to add the BOLForm object as an attribute of the model:

     @GetMapping("/BillOfLading")
        public String getBOLForm(Model model){
         model.addAttribute("BOLForm", new BOLForm());
            return "bol";
        }