[英]Spring | Hibernate | Thymeleaf: Neither BindingResult nor plain target object for bean name 'id' available as request attribute
[英]ThymeLeaf: Neither BindingResult nor plain target object for bean name available as request attribute
我正在嘗試在 thymeleaf 中構建一個簡單的 select 和選項列表,但我一直收到錯誤消息
Bean 名稱“BOLForm”的 BindingResult 和普通目標 object 都不能用作請求屬性
我很確定我的bol.html有問題。 但無法找出丟失的映射。
下面是我的 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";
}
}
表格 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;
}
}
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>
當我如下更換我的 bol.html 時,它可以工作:
<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/>
在 controller 中,您需要添加BOLForm
object 作為 model 的屬性:
@GetMapping("/BillOfLading")
public String getBOLForm(Model model){
model.addAttribute("BOLForm", new BOLForm());
return "bol";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.